Browse Source

[core] Add li_memory_usage(), clean up some old sys-* files

personal/stbuehler/wip
Thomas Porzelt 12 years ago
parent
commit
20c3e7d6ad
  1. 12
      include/lighttpd/settings.h
  2. 84
      include/lighttpd/sys-files.h
  3. 26
      include/lighttpd/sys-mmap.h
  4. 21
      include/lighttpd/sys-process.h
  5. 7
      include/lighttpd/sys_memory.h
  6. 0
      include/lighttpd/sys_socket.h
  7. 2
      include/lighttpd/utils.h
  8. 4
      src/CMakeLists.txt
  9. 4
      src/common/Makefile.am
  10. 64
      src/common/sys-files.c
  11. 115
      src/common/sys_memory.c
  12. 2
      src/common/sys_socket.c
  13. 2
      src/common/utils.c
  14. 1
      src/common/wscript

12
include/lighttpd/settings.h

@ -83,6 +83,12 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#if defined(LIGHTY_OS_LINUX)
# include <sched.h>
#endif
/* on linux 2.4.x you get either sendfile or LFS */
#if defined(LIGHTY_OS_LINUX) && defined(HAVE_SYS_SENDFILE_H) && defined(HAVE_SENDFILE) && (defined(_LARGEFILE_SOURCE) || defined(HAVE_SENDFILE64)) && defined(HAVE_WRITEV) && !defined(HAVE_SENDFILE_BROKEN)
@ -206,10 +212,8 @@
# define PACKAGE_BUILD_DATE (__DATE__ " " __TIME__)
#endif
#include <lighttpd/sys-files.h>
#include <lighttpd/sys-mmap.h>
#include <lighttpd/sys-process.h>
#include <lighttpd/sys-socket.h>
#include <lighttpd/sys_memory.h>
#include <lighttpd/sys_socket.h>
#if( 2 < __GNUC__ )
#pragma GCC poison strtok asctime ctime tmpnam strerror

84
include/lighttpd/sys-files.h

@ -1,84 +0,0 @@
#ifndef _SYS_FILES_H_
#define _SYS_FILES_H_
#define DIR_SEPERATOR_UNIX '/'
#define DIR_SEPERATOR_UNIX_STR "/"
#define DIR_SEPERATOR_WIN '\\'
#define DIR_SEPERATOR_WIN_STR "\\"
#include <lighttpd/settings.h>
#ifdef _WIN32
#include <windows.h>
#include <io.h> /* open */
#include <direct.h> /* chdir */
#define DIR_SEPERATOR DIR_SEPERATOR_WIN
#define DIR_SEPERATOR_STR DIR_SEPERATOR_WIN_STR
#define __S_ISTYPE(mode, mask) (((mode) & _S_IFMT) == (mask))
#undef S_ISDIR
#undef S_ISCHR
#undef S_ISBLK
#undef S_ISREG
#define S_ISDIR(mode) __S_ISTYPE((mode), _S_IFDIR)
#define S_ISCHR(mode) __S_ISTYPE((mode), _S_IFCHR)
#define S_ISBLK(mode) __S_ISTYPE((mode), _S_IFBLK)
#define S_ISREG(mode) __S_ISTYPE((mode), _S_IFREG)
/* we don't support symlinks */
#define S_ISLNK(mode) 0
#define lstat stat
#define mkstemp(x) open(mktemp(x), O_RDWR)
#define mkdir(x, y) mkdir(x)
/* retrieve the most recent network, or general libc error */
#define light_sock_errno() (WSAGetLastError())
struct dirent {
const char *d_name;
};
typedef struct {
HANDLE h;
WIN32_FIND_DATA finddata;
struct dirent dent;
} DIR;
LI_EXPORT DIR * opendir(const char *dn);
LI_EXPORT struct dirent * readdir(DIR *d);
LI_EXPORT void closedir(DIR *d);
LI_EXPORT GString * filename_unix2local(GString *b);
LI_EXPORT GString * pathname_unix2local(GString *b);
#else /* _WIN32 */
#include <unistd.h>
#include <dirent.h>
#define DIR_SEPERATOR DIR_SEPERATOR_UNIX
#define DIR_SEPERATOR_STR DIR_SEPERATOR_UNIX_STR
#define light_sock_errno() (errno)
#define filename_unix2local(x) /* (x) */
#define pathname_unix2local(x) /* (x) */
#endif /* _WIN32 */
#define PATHNAME_APPEND_SLASH(x) \
if (x->len > 1 && x->ptr[x->len - 1] != DIR_SEPERATOR) { \
g_string_append_c(DIR_SEPEARATOR); \
}
#ifndef O_LARGEFILE
# define O_LARGEFILE 0
#endif
#ifndef O_NOATIME
# define O_NOATIME 0
#endif
#endif

26
include/lighttpd/sys-mmap.h

@ -1,26 +0,0 @@
#ifndef WIN32_MMAP_H
#define WIN32_MMAP_H
#include <lighttpd/settings.h>
#ifdef _WIN32
#define MAP_FAILED -1
#define PROT_SHARED 0
#define MAP_SHARED 0
#define PROT_READ 0
#define mmap(a, b, c, d, e, f) (-1)
#define munmap(a, b) (-1)
#include <windows.h>
#else
#include <sys/mman.h>
#ifndef MAP_FAILED
#define MAP_FAILED -1
#endif
#endif
#endif

21
include/lighttpd/sys-process.h

@ -1,21 +0,0 @@
#ifndef _SYS_PROCESS_H_
#define _SYS_PROCESS_H_
#ifdef _WIN32
#include <process.h>
#define pid_t int
/* win32 has no fork() */
#define kill(x, y) do { } while (0)
#define getpid() 0
#else
#include <sys/wait.h>
#include <unistd.h>
#endif
#ifdef LIGHTY_OS_LINUX
#include <sched.h>
#endif
#endif

7
include/lighttpd/sys_memory.h

@ -0,0 +1,7 @@
#ifndef _LIGHTTPD_SYS_MEMORY_H_
#define _LIGHTTPD_SYS_MEMORY_H_
/* returns the currently used memory (RSS, resident set size) in bytes or 0 on failure */
LI_API gsize li_memory_usage(void);
#endif

0
include/lighttpd/sys-socket.h → include/lighttpd/sys_socket.h

2
include/lighttpd/utils.h

@ -18,7 +18,7 @@ LI_API void li_fd_init(int fd);
LI_API void li_fd_no_block(int fd);
LI_API void li_fd_block(int fd);
#ifndef _WIN32
#ifndef LIGHTY_OS_WINDOWS
/* return -2 for EAGAIN, -1 for some other error, 0 for success */
LI_API int li_send_fd(int s, int fd); /* write fd to unix socket s */
LI_API int li_receive_fd(int s, int *fd); /* read fd from unix socket s */

4
src/CMakeLists.txt

@ -176,8 +176,8 @@ SET(COMMON_SRC
mempool.c
module.c
radix.c
sys-files.c
sys-socket.c
sys_memory.c
sys_socket.c
utils.c
waitqueue.c
)

4
src/common/Makefile.am

@ -12,8 +12,8 @@ common_src= \
mempool.c \
module.c \
radix.c \
sys-files.c \
sys-socket.c \
sys_memory.c \
sys_socket.c \
utils.c \
waitqueue.c

64
src/common/sys-files.c

@ -1,64 +0,0 @@
#include <lighttpd/sys-files.h>
#ifdef _WIN32
DIR *opendir(const char *dn) {
DIR *d = g_slice_new0(DIR);
if (INVALID_HANDLE_VALUE == (d->h = FindFirstFile(dn, &(d->finddata)))) {
return NULL;
}
return d;
}
struct dirent *readdir(DIR *d) {
if (!d->dent.d_name) {
/* opendir has set a finddata already, push it out */
d->dent.d_name = d->finddata.cFileName;
return &(d->dent);
}
if (FindNextFile(d->h, &(d->finddata))) {
d->dent.d_name = d->finddata.cFileName;
return &(d->dent);
} else {
return NULL;
}
}
void closedir(DIR *d) {
FindClose(d);
g_slice_free(DIR, d);
}
GString *pathname_unix2local(GString *fn) {
size_t i;
for (i = 0; i < fn->len; i++) {
if (fn->str[i] == '/') {
fn->str[i] = '\\';
}
}
return fn;
}
GString *filename_unix2local(GString *fn) {
size_t i;
for (i = 0; i < fn->len; i++) {
if (fn->str[i] == '/') {
fn->str[i] = '\\';
}
}
#if 0
buffer_prepare_append(fn, 4);
memmove(fn->ptr + 4, fn->ptr, fn->used);
memcpy(fn->ptr, "\\\\?\\", 4);
fn->used += 4;
#endif
return fn;
}
#endif

115
src/common/sys_memory.c

@ -0,0 +1,115 @@
#include <lighttpd/base.h>
#if defined(LIGHTY_OS_LINUX)
#include <fcntl.h>
gsize li_memory_usage(void) {
/* parse /proc/self/stat */
int d;
gchar s[PATH_MAX];
gchar c;
unsigned int u;
long unsigned int lu;
long int ld;
long long unsigned int llu;
gsize rss;
FILE *f;
f = fopen("/proc/self/stat", "r");
if (!f)
return 0;
/* fields according to proc(5) */
d = fscanf(
f,
"%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu"
" %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld",
&d, s, &c, &d, &d, &d, &d, &d, &u, &lu, &lu, &lu, &lu, &lu, &lu, &ld, &ld, &ld, &ld, &ld, &ld, &lu, &lu,
(long int*)&rss, &lu, &lu, &lu, &lu, &lu, &lu, &lu, &lu, &lu, &lu, &lu, &lu, &lu, &d, &d, &u, &u, &llu, &lu, &ld
);
fclose(f);
/* rss is the 24th field */
if (d < 24)
return 0;
return rss * getpagesize();
}
#elif defined(LIGHTY_OS_FREEBSD)
#include <paths.h>
#include <fcntl.h>
#include <kvm.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/user.h>
gsize li_memory_usage(void) {
kvm_t *kvm;
struct kinfo_proc *ki_proc;
gint cnt;
gsize rss;
kvm = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
if (!kvm)
return 0;
ki_proc = kvm_getprocs(kvm, KERN_PROC_PID, getpid(), &cnt);
if (!ki_proc) {
kvm_close(kvm);
return 0;
}
rss = ki_proc->ki_rssize * getpagesize();
kvm_close(kvm);
return rss;
}
#elif defined(LIGHTY_OS_MACOSX)
#include <mach/task.h>
#include <mach/mach_init.h>
gsize li_memory_usage(void) {
/* info from http://miknight.blogspot.com/2005/11/resident-set-size-in-mac-os-x.html */
struct task_basic_info tbinfo;
mach_msg_type_number_t cnt = TASK_BASIC_INFO_COUNT;
if (KERN_SUCCESS != task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t) &tbinfo, &cnt))
return 0;
return binfo.resident_size;
}
#elif defined(LIGHTY_OS_SOLARIS)
gsize li_memory_usage(void) {
/* /proc/$pid/psinfo: http://docs.sun.com/app/docs/doc/816-5174/proc-4?l=en&a=view */
gchar path[64];
psinfo_t psinfo;
FILE *f;
sprintf(path, "/proc/%d/psinfo", getpid());
f = fopen(path, "r");
if (!f)
return 0;
if (fread(&psinfo, sizeof(psinfo_t), 1, f) != 1) {
fclose(f);
return 0;
}
return psinfo.pr_rssize * 1024;
}
#else
gsize li_memory_usage(void) {
/* unsupported OS */
return 0;
}
#endif

2
src/common/sys-socket.c → src/common/sys_socket.c

@ -1,5 +1,5 @@
#include <lighttpd/base.h>
#include <lighttpd/sys-socket.h>
#include <lighttpd/sys_socket.h>
#ifndef HAVE_INET_ATON
/* win32 has inet_addr instead if inet_aton */

2
src/common/utils.c

@ -818,7 +818,7 @@ gsize li_dirent_buf_size(DIR * dirp) {
}
const char *li_remove_path(const char *path) {
char *p = strrchr(path, DIR_SEPERATOR);
char *p = strrchr(path, G_DIR_SEPARATOR);
if (NULL != p && *(p) != '\0') {
return (p + 1);
}

1
src/common/wscript

@ -25,6 +25,7 @@ def build(bld):
mempool.c
module.c
radix.c
sys_memory.c
utils.c
waitqueue.c
'''

Loading…
Cancel
Save