From 295c8df2c1bcae3f3afd60a45ec287eb6d644212 Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Thu, 22 Oct 2015 21:26:51 +0200 Subject: [PATCH] mx_proc: Move process related function from mx_util to mx_proc --- .gitignore | 1 + Makefile | 13 ++++++ mx_proc.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++ mx_proc.h | 58 ++++++++++++++++++++++++++ mx_util.c | 98 ------------------------------------------- mx_util.h | 51 ----------------------- mxqd.c | 8 ++-- test_mx_util.c | 24 ++++------- 8 files changed, 196 insertions(+), 168 deletions(-) create mode 100644 mx_proc.c create mode 100644 mx_proc.h diff --git a/.gitignore b/.gitignore index d6af43f..f466977 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ mx_getopt.o mx_flock.o mx_log.o mx_util.o +mx_proc.o mxq_group.o mxqadmin.o mxqdump.o diff --git a/Makefile b/Makefile index dad0d7d..ca31600 100644 --- a/Makefile +++ b/Makefile @@ -242,6 +242,10 @@ mx_log.h += mx_log.h mx_util.h += mx_util.h +### mx_proc.h ---------------------------------------------------------- + +mx_proc.h += mx_proc.h + ### mx_flock.h --------------------------------------------------------- mx_flock.h += mx_flock.h @@ -292,6 +296,12 @@ mx_util.o: $(mx_log.h) clean: CLEAN += mx_util.o +### mx_proc ------------------------------------------------------------ + +mx_proc.o: $(mx_proc.h) + +clean: CLEAN += mx_proc.o + ### mx_flock.o --------------------------------------------------------- mx_flock.o: $(mx_flock.h) @@ -373,6 +383,7 @@ clean: CLEAN += mxq_job.o mxqd.o: $(mx_getopt.h) mxqd.o: $(mx_flock.h) mxqd.o: $(mx_util.h) +mxqd.o: $(mx_proc.h) mxqd.o: $(mx_log.h) mxqd.o: $(mxqd.h) mxqd.o: $(mxq_group.h) @@ -405,6 +416,7 @@ clean: CLEAN += mxqsub.o mxqd: mx_flock.o mxqd: mx_util.o +mxqd: mx_proc.o mxqd: mx_log.o mxqd: mxq_log.o mxqd: mx_getopt.o @@ -522,6 +534,7 @@ test_mx_util.o: $(mx_util.h) clean: CLEAN += test_mx_util.o test_mx_util: mx_util.o +test_mx_util: mx_proc.o test_mx_util: mx_log.o clean: CLEAN += test_mx_util diff --git a/mx_proc.c b/mx_proc.c new file mode 100644 index 0000000..2af1a5d --- /dev/null +++ b/mx_proc.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include + +#include "mx_util.h" +#include "mx_proc.h" + +static int _mx_proc_pid_stat_strscan(char *str, struct mx_proc_pid_stat *pps) +{ + size_t res = 0; + char *p; + char *s; + + pps->comm = NULL; + + s = str; + + res += mx_strscan_ll(&s, &(pps->pid)); + + p = strrchr(s, ')'); + if (!p) + return -(errno=EINVAL); + + *p = 0; + s++; + + pps->comm = mx_strdup_forever(s); + s = p + 2; + + pps->state = *s; + res += !(*(s+1) == ' '); + s += 2; + + res += mx_strscan_ll(&s, &(pps->ppid)); + res += mx_strscan_ll(&s, &(pps->pgrp)); + res += mx_strscan_ll(&s, &(pps->session)); + res += mx_strscan_ll(&s, &(pps->tty_nr)); + res += mx_strscan_ll(&s, &(pps->tpgid)); + res += mx_strscan_ull(&s, &(pps->flags)); + res += mx_strscan_ull(&s, &(pps->minflt)); + res += mx_strscan_ull(&s, &(pps->cminflt)); + res += mx_strscan_ull(&s, &(pps->majflt)); + res += mx_strscan_ull(&s, &(pps->cmajflt)); + res += mx_strscan_ull(&s, &(pps->utime)); + res += mx_strscan_ull(&s, &(pps->stime)); + res += mx_strscan_ll(&s, &(pps->cutime)); + res += mx_strscan_ll(&s, &(pps->cstime)); + res += mx_strscan_ll(&s, &(pps->priority)); + res += mx_strscan_ll(&s, &(pps->nice)); + res += mx_strscan_ll(&s, &(pps->num_threads)); + res += mx_strscan_ll(&s, &(pps->itrealvalue)); + res += mx_strscan_ull(&s, &(pps->starttime)); + res += mx_strscan_ull(&s, &(pps->vsize)); + res += mx_strscan_ll(&s, &(pps->rss)); + res += mx_strscan_ull(&s, &(pps->rsslim)); + res += mx_strscan_ull(&s, &(pps->startcode)); + res += mx_strscan_ull(&s, &(pps->endcode)); + res += mx_strscan_ull(&s, &(pps->startstack)); + res += mx_strscan_ull(&s, &(pps->kstkesp)); + res += mx_strscan_ull(&s, &(pps->kstkeip)); + res += mx_strscan_ull(&s, &(pps->signal)); + res += mx_strscan_ull(&s, &(pps->blocked)); + res += mx_strscan_ull(&s, &(pps->sigignore)); + res += mx_strscan_ull(&s, &(pps->sigcatch)); + res += mx_strscan_ull(&s, &(pps->wchan)); + res += mx_strscan_ull(&s, &(pps->nswap)); + res += mx_strscan_ull(&s, &(pps->cnswap)); + res += mx_strscan_ll(&s, &(pps->exit_signal)); + res += mx_strscan_ll(&s, &(pps->processor)); + res += mx_strscan_ull(&s, &(pps->rt_priority)); + res += mx_strscan_ull(&s, &(pps->policy)); + res += mx_strscan_ull(&s, &(pps->delayacct_blkio_ticks)); + res += mx_strscan_ull(&s, &(pps->guest_time)); + res += mx_strscan_ll(&s, &(pps->cguest_time)); + + if (res != 0) + return -(errno=EINVAL); + + return 0; +} + +int mx_proc_pid_stat(struct mx_proc_pid_stat **pps, pid_t pid) +{ + _mx_cleanup_free_ char *fname = NULL; + _mx_cleanup_free_ char *line = NULL; + int res; + + mx_asprintf_forever(&fname, "/proc/%d/stat", pid); + + if (!*pps) + *pps = mx_calloc_forever(1, sizeof(**pps)); + + res = mx_read_first_line_from_file(fname, &line); + if (res < 0) + return res; + + res = _mx_proc_pid_stat_strscan(line, *pps); + if (res < 0) + return res; + + return 0; +} + +void mx_proc_pid_stat_free_content(struct mx_proc_pid_stat *pps) +{ + if (!pps) + return; + + mx_free_null(pps->comm); +} diff --git a/mx_proc.h b/mx_proc.h new file mode 100644 index 0000000..4a0efb8 --- /dev/null +++ b/mx_proc.h @@ -0,0 +1,58 @@ +#ifndef __MX_PROC_H__ +#define __MX_PROC_H__ 1 + +#include + +struct mx_proc_pid_stat { + long long int pid; /* 1 */ + char *comm; /* 2 (comm) */ + char state; /* 3 "RSDZTW" */ + long long int ppid; /* 4 */ + long long int pgrp; /* 5 */ + long long int session; /* 6 */ + long long int tty_nr; /* 7 */ + long long int tpgid; /* 8 */ + unsigned long long int flags; /* 9 */ + unsigned long long int minflt; /* 10 */ + unsigned long long int cminflt; /* 11 */ + unsigned long long int majflt; /* 12 */ + unsigned long long int cmajflt; /* 13 */ + unsigned long long int utime; /* 14 */ + unsigned long long int stime; /* 15 */ + long long int cutime; /* 16 */ + long long int cstime; /* 17 */ + long long int priority; /* 18 */ + long long int nice; /* 19 */ + long long int num_threads; /* 20 */ + long long int itrealvalue; /* 21 */ + unsigned long long int starttime; /* 22 */ + unsigned long long int vsize; /* 23 */ + long long int rss; /* 24 */ + unsigned long long int rsslim; /* 25 */ + unsigned long long int startcode; /* 26 */ + unsigned long long int endcode; /* 27 */ + unsigned long long int startstack; /* 28 */ + unsigned long long int kstkesp; /* 29 */ + unsigned long long int kstkeip; /* 30 */ + unsigned long long int signal; /* 31 */ + unsigned long long int blocked; /* 32 */ + unsigned long long int sigignore; /* 33 */ + unsigned long long int sigcatch; /* 34 */ + unsigned long long int wchan; /* 35 */ + unsigned long long int nswap; /* 36 */ + unsigned long long int cnswap; /* 37 */ + long long int exit_signal; /* 38 */ + long long int processor; /* 39 */ + unsigned long long int rt_priority; /* 40 */ + unsigned long long int policy; /* 41 */ + unsigned long long int delayacct_blkio_ticks; /* 42 */ + unsigned long long int guest_time; /* 43 */ + long long int cguest_time; /* 44 */ +}; + +int mx_proc_pid_stat_read(struct mx_proc_pid_stat *pps, char *fmt, ...); +int mx_proc_pid_stat(struct mx_proc_pid_stat **pps, pid_t pid); + +void mx_proc_pid_stat_free_content(struct mx_proc_pid_stat *pps); + +#endif diff --git a/mx_util.c b/mx_util.c index 24088b2..f783ed3 100644 --- a/mx_util.c +++ b/mx_util.c @@ -856,104 +856,6 @@ int mx_strscan_ll(char **str, long long int *to) return res; } -int mx_strscan_proc_pid_stat(char *str, struct proc_pid_stat *pps) -{ - size_t res = 0; - char *p; - char *s; - - pps->comm = NULL; - - s = str; - - res += mx_strscan_ll(&s, &(pps->pid)); - - p = strrchr(s, ')'); - if (!p) - return -(errno=EINVAL); - - *p = 0; - s++; - - pps->comm = mx_strdup_forever(s); - s = p + 2; - - pps->state = *s; - res += !(*(s+1) == ' '); - s += 2; - - res += mx_strscan_ll(&s, &(pps->ppid)); - res += mx_strscan_ll(&s, &(pps->pgrp)); - res += mx_strscan_ll(&s, &(pps->session)); - res += mx_strscan_ll(&s, &(pps->tty_nr)); - res += mx_strscan_ll(&s, &(pps->tpgid)); - res += mx_strscan_ull(&s, &(pps->flags)); - res += mx_strscan_ull(&s, &(pps->minflt)); - res += mx_strscan_ull(&s, &(pps->cminflt)); - res += mx_strscan_ull(&s, &(pps->majflt)); - res += mx_strscan_ull(&s, &(pps->cmajflt)); - res += mx_strscan_ull(&s, &(pps->utime)); - res += mx_strscan_ull(&s, &(pps->stime)); - res += mx_strscan_ll(&s, &(pps->cutime)); - res += mx_strscan_ll(&s, &(pps->cstime)); - res += mx_strscan_ll(&s, &(pps->priority)); - res += mx_strscan_ll(&s, &(pps->nice)); - res += mx_strscan_ll(&s, &(pps->num_threads)); - res += mx_strscan_ll(&s, &(pps->itrealvalue)); - res += mx_strscan_ull(&s, &(pps->starttime)); - res += mx_strscan_ull(&s, &(pps->vsize)); - res += mx_strscan_ll(&s, &(pps->rss)); - res += mx_strscan_ull(&s, &(pps->rsslim)); - res += mx_strscan_ull(&s, &(pps->startcode)); - res += mx_strscan_ull(&s, &(pps->endcode)); - res += mx_strscan_ull(&s, &(pps->startstack)); - res += mx_strscan_ull(&s, &(pps->kstkesp)); - res += mx_strscan_ull(&s, &(pps->kstkeip)); - res += mx_strscan_ull(&s, &(pps->signal)); - res += mx_strscan_ull(&s, &(pps->blocked)); - res += mx_strscan_ull(&s, &(pps->sigignore)); - res += mx_strscan_ull(&s, &(pps->sigcatch)); - res += mx_strscan_ull(&s, &(pps->wchan)); - res += mx_strscan_ull(&s, &(pps->nswap)); - res += mx_strscan_ull(&s, &(pps->cnswap)); - res += mx_strscan_ll(&s, &(pps->exit_signal)); - res += mx_strscan_ll(&s, &(pps->processor)); - res += mx_strscan_ull(&s, &(pps->rt_priority)); - res += mx_strscan_ull(&s, &(pps->policy)); - res += mx_strscan_ull(&s, &(pps->delayacct_blkio_ticks)); - res += mx_strscan_ull(&s, &(pps->guest_time)); - res += mx_strscan_ll(&s, &(pps->cguest_time)); - - if (res != 0) - return -(errno=EINVAL); - - return 0; -} - -int mx_proc_pid_stat(struct proc_pid_stat *pps, pid_t pid) -{ - _mx_cleanup_free_ char *fname = NULL; - _mx_cleanup_free_ char *line = NULL; - int res; - - mx_asprintf_forever(&fname, "/proc/%d/stat", pid); - - res = mx_read_first_line_from_file(fname, &line); - if (res < 0) - return res; - - res = mx_strscan_proc_pid_stat(line, pps); - if (res < 0) - return res; - - return 0; -} - -void mx_proc_pid_stat_free(struct proc_pid_stat *pps) -{ - mx_free_null(pps->comm); -} - int mx_sleep(unsigned int seconds) { if (seconds) diff --git a/mx_util.h b/mx_util.h index aeb2489..16a27d3 100644 --- a/mx_util.h +++ b/mx_util.h @@ -10,53 +10,6 @@ #include "mx_log.h" -struct proc_pid_stat { - long long int pid; /* 1 */ - char *comm; /* 2 (comm) */ - char state; /* 3 "RSDZTW" */ - long long int ppid; /* 4 */ - long long int pgrp; /* 5 */ - long long int session; /* 6 */ - long long int tty_nr; /* 7 */ - long long int tpgid; /* 8 */ - unsigned long long int flags; /* 9 */ - unsigned long long int minflt; /* 10 */ - unsigned long long int cminflt; /* 11 */ - unsigned long long int majflt; /* 12 */ - unsigned long long int cmajflt; /* 13 */ - unsigned long long int utime; /* 14 */ - unsigned long long int stime; /* 15 */ - long long int cutime; /* 16 */ - long long int cstime; /* 17 */ - long long int priority; /* 18 */ - long long int nice; /* 19 */ - long long int num_threads; /* 20 */ - long long int itrealvalue; /* 21 */ - unsigned long long int starttime; /* 22 */ - unsigned long long int vsize; /* 23 */ - long long int rss; /* 24 */ - unsigned long long int rsslim; /* 25 */ - unsigned long long int startcode; /* 26 */ - unsigned long long int endcode; /* 27 */ - unsigned long long int startstack; /* 28 */ - unsigned long long int kstkesp; /* 29 */ - unsigned long long int kstkeip; /* 30 */ - unsigned long long int signal; /* 31 */ - unsigned long long int blocked; /* 32 */ - unsigned long long int sigignore; /* 33 */ - unsigned long long int sigcatch; /* 34 */ - unsigned long long int wchan; /* 35 */ - unsigned long long int nswap; /* 36 */ - unsigned long long int cnswap; /* 37 */ - long long int exit_signal; /* 38 */ - long long int processor; /* 39 */ - unsigned long long int rt_priority; /* 40 */ - unsigned long long int policy; /* 41 */ - unsigned long long int delayacct_blkio_ticks; /* 42 */ - unsigned long long int guest_time; /* 43 */ - long long int cguest_time; /* 44 */ -}; - #ifdef MX_NDEBUG # include # define mx_assert_return_minus_errno(test, eno) \ @@ -168,10 +121,6 @@ int mx_read_first_line_from_file(char *fname, char **line); int mx_strscan_ull(char **str, unsigned long long int *to); int mx_strscan_ll(char **str, long long int *to); -int mx_strscan_proc_pid_stat(char *str, struct proc_pid_stat *pps); - -int mx_proc_pid_stat(struct proc_pid_stat *pps, pid_t pid); -void mx_proc_pid_stat_free(struct proc_pid_stat *pps); int mx_sleep(unsigned int seconds); int mx_sleep_nofail(unsigned int seconds); diff --git a/mxqd.c b/mxqd.c index d4e5372..77706c7 100644 --- a/mxqd.c +++ b/mxqd.c @@ -34,6 +34,7 @@ #include "mxq_group.h" #include "mxq_job.h" #include "mx_mysql.h" +#include "mx_proc.h" #include "mxqd.h" #include "mxq.h" @@ -281,7 +282,8 @@ int server_init(struct mxq_server *server, int argc, char *argv[]) unsigned long memory_total = 2048; unsigned long memory_max = 0; int i; - struct proc_pid_stat pps = {0}; + + _mx_cleanup_free_ struct mx_proc_pid_stat *pps = NULL; struct mx_getopt_ctl optctl; struct mx_option opts[] = { @@ -503,8 +505,8 @@ int server_init(struct mxq_server *server, int argc, char *argv[]) res = mx_proc_pid_stat(&pps, getpid()); assert(res == 0); - server->starttime = pps.starttime; - mx_proc_pid_stat_free(&pps); + server->starttime = pps->starttime; + mx_proc_pid_stat_free_content(pps); mx_asprintf_forever(&server->host_id, "%s-%llx-%x", server->boot_id, server->starttime, getpid()); mx_setenv_forever("MXQ_HOSTID", server->host_id); diff --git a/test_mx_util.c b/test_mx_util.c index b290490..817fbef 100644 --- a/test_mx_util.c +++ b/test_mx_util.c @@ -8,6 +8,7 @@ #include #include "mx_util.h" +#include "mx_proc.h" static void test_mx_strskipwhitespaces(void) { @@ -305,8 +306,7 @@ static void test_mx_strscan(void) unsigned long long int ull; long long int ll; _mx_cleanup_free_ char *line = NULL; - struct proc_pid_stat pps = {0}; - struct proc_pid_stat pps2 = {0}; + _mx_cleanup_free_ struct mx_proc_pid_stat *pps = NULL; assert(s = strdup("123 456 -789 246 abc")); str = s; @@ -340,20 +340,12 @@ static void test_mx_strscan(void) assert(mx_streq(str, "")); assert(mx_streq(s, "123")); - assert(mx_read_first_line_from_file("/proc/self/stat", &line) > 0); - assert(mx_strscan_proc_pid_stat(line, &pps) == 0); - assert(pps.pid == getpid()); - assert(pps.ppid == getppid()); - assert(pps.state == 'R'); - assert(mx_streq(pps.comm, program_invocation_short_name) || mx_streq(pps.comm, "memcheck-amd64-")); - mx_proc_pid_stat_free(&pps); - - assert(mx_proc_pid_stat(&pps2, getpid()) == 0); - assert(pps2.pid == getpid()); - assert(pps2.ppid == getppid()); - assert(pps2.state == 'R'); - assert(mx_streq(pps2.comm, program_invocation_short_name) || mx_streq(pps2.comm, "memcheck-amd64-")); - mx_proc_pid_stat_free(&pps2); + assert(mx_proc_pid_stat(&pps, getpid()) == 0); + assert(pps->pid == getpid()); + assert(pps->ppid == getppid()); + assert(pps->state == 'R'); + assert(mx_streq(pps->comm, program_invocation_short_name) || mx_streq(pps->comm, "memcheck-amd64-")); + mx_proc_pid_stat_free(pps); } static void test_mx_strvec() {