X-Git-Url: http://git.linux-vserver.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=util-vserver%2Fsrc%2Fvserver-stat.c;h=32f6d24747a05149a4aad848e80669f2b2688a89;hb=7da0252aafaf7e61f1fd01645b629e562612edf3;hp=9c6c398328b78c3a5f5be59085249853cd2e8954;hpb=4c0037aecd96cf23da727c12381723ab5cbfa5e4;p=util-vserver.git diff --git a/util-vserver/src/vserver-stat.c b/util-vserver/src/vserver-stat.c index 9c6c398..32f6d24 100644 --- a/util-vserver/src/vserver-stat.c +++ b/util-vserver/src/vserver-stat.c @@ -38,6 +38,8 @@ #include "util.h" #include "internal.h" #include "wrappers.h" +#include "wrappers-dirent.h" +#include "wrappers-vserver.h" #include #include @@ -61,7 +63,7 @@ int wrapper_exit_code = 1; struct ctx_list { - int ctx; + xid_t ctx; int process_count; int VmSize_total; int VmRSS_total; @@ -78,7 +80,7 @@ struct process_info long start_time; // start time of process -- seconds since 1-1-70 long stime, utime; // kernel & user-mode CPU time accumulated by process long cstime, cutime; // cumulative time of process and reaped children - int s_context; + xid_t s_context; }; char *process_name; @@ -203,35 +205,21 @@ add_ctx(struct ctx_list *list, struct process_info *process) // increment the count number in the ctx record using ctx number static void -count_ctx(struct ctx_list *list, struct process_info *process) +count_ctx(struct ctx_list **ptr, struct process_info *process) { - struct ctx_list *prev = list; + for (;;) { + if (*ptr==0 || (*ptr)->ctx > process->s_context) { + *ptr = insert_ctx(process->s_context, *ptr); + break; + } - if (process == NULL) return; + if ((*ptr)->ctx == process->s_context) + break; - // search - while(list != NULL) - { - // find - if (list->ctx == process->s_context) - { - add_ctx(list, process); - return; - } - // insert between - if (list->ctx > process->s_context) - { - prev->next = insert_ctx(process->s_context, list); - add_ctx(prev->next, process); - return; - } - // ++ - prev = list; - list = list->next; - } - // add at the end - prev->next = insert_ctx(process->s_context, NULL); - add_ctx(prev->next, process); + ptr = &(*ptr)->next; + } + + add_ctx(*ptr, process); } // free mem @@ -251,14 +239,22 @@ free_ctx(struct ctx_list *list) // open the process's status file to get the ctx number, and other stat struct process_info *get_process_info(char *pid) { - int fd; - char buffer[1024]; - char *p; - size_t idx, l=strlen(pid); - static struct process_info process; - - process.s_context = vc_X_getctx(atoi(pid)); - + int fd; + char buffer[1024]; + char *p; + size_t idx, l=strlen(pid); + static struct process_info process; + + process.s_context = vc_get_task_xid(atoi(pid)); + + if (process.s_context==VC_NOCTX) { + int err=errno; + WRITE_MSG(2, "vc_get_task_xid("); + WRITE_STR(2, pid); + WRITE_MSG(2, "): "); + WRITE_STR(2, strerror(err)); + WRITE_MSG(2, "\n"); + } memcpy(buffer, "/proc/", 6); idx = 6; memcpy(buffer+idx, pid, l); idx += l; @@ -391,13 +387,13 @@ void showContexts(struct ctx_list *list) WRITE_MSG(1, "CTX PROC VSZ RSS userTIME sysTIME UPTIME NAME\n"); while (list!=0) { - char buf[512]; - char tmp[32]; + char buf[sizeof(xid_t)*3 + 512]; + char tmp[sizeof(int)*3 + 2]; size_t l; memset(buf, ' ', sizeof(buf)); - l = utilvserver_fmt_ulong(buf, list->ctx); - l = utilvserver_fmt_ulong(tmp, list->process_count); + l = utilvserver_fmt_long(buf, list->ctx); + l = utilvserver_fmt_long(tmp, list->process_count); memcpy(buf+10-l, tmp, l); shortenMem (buf+10, list->VmSize_total); @@ -452,11 +448,8 @@ int main(int argc, char **argv) #if 1 // try to switch in context 1 - if (vc_new_s_context(1,0, 0) < 0) - { - perror("vc_new_s_context(#1,...)"); - return EXIT_FAILURE; - } + if (vc_get_task_xid(0)!=1) + Evc_new_s_context(1, 0,0); #endif // create the fist... @@ -464,13 +457,9 @@ int main(int argc, char **argv) // init with the default name for the context 0 strncpy(my_ctx_list->name, "root server", CTX_NAME_MAX_LEN); - // open the /proc dir - if ((proc_dir = opendir(PROC_DIR_NAME)) == NULL) { - perror("opendir()"); - return EXIT_FAILURE; - } - - chdir(PROC_DIR_NAME); + + Echdir(PROC_DIR_NAME); + proc_dir = Eopendir("."); while ((dir_entry = readdir(proc_dir)) != NULL) { // select only process file @@ -478,7 +467,7 @@ int main(int argc, char **argv) continue; if (atoi(dir_entry->d_name) != my_pid) - count_ctx(my_ctx_list, get_process_info(dir_entry->d_name)); + count_ctx(&my_ctx_list, get_process_info(dir_entry->d_name)); } closedir(proc_dir);