PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/install/procstat.c

https://github.com/rsd/artica-1.5
C | 240 lines | 174 code | 28 blank | 38 comment | 5 complexity | 034922823379993ffda05c608d17637a MD5 | raw file
  1. /*
  2. * Displays linux /proc/pid/stat in human-readable format
  3. *
  4. * Build: gcc -o procstat procstat.c
  5. * Usage: procstat pid
  6. * cat /proc/pid/stat | procstat
  7. *
  8. * Homepage: http://www.brokestream.com/procstat.html
  9. * Version : 2009-03-05
  10. *
  11. * Ivan Tikhonov, http://www.brokestream.com, kefeer@netangels.ru
  12. *
  13. * 2007-09-19 changed HZ=100 error to warning
  14. *
  15. * 2009-03-05 tickspersec are taken from sysconf (Sabuj Pattanayek)
  16. *
  17. */
  18. /* Copyright (C) 2009 Ivan Tikhonov
  19. This software is provided 'as-is', without any express or implied
  20. warranty. In no event will the authors be held liable for any damages
  21. arising from the use of this software.
  22. Permission is granted to anyone to use this software for any purpose,
  23. including commercial applications, and to alter it and redistribute it
  24. freely, subject to the following restrictions:
  25. 1. The origin of this software must not be misrepresented; you must not
  26. claim that you wrote the original software. If you use this software
  27. in a product, an acknowledgment in the product documentation would be
  28. appreciated but is not required.
  29. 2. Altered source versions must be plainly marked as such, and must not be
  30. misrepresented as being the original software.
  31. 3. This notice may not be removed or altered from any source distribution.
  32. Ivan Tikhonov, kefeer@brokestream.com
  33. */
  34. #include <stdio.h>
  35. #include <unistd.h>
  36. #include <time.h>
  37. #include <linux/limits.h>
  38. #include <sys/times.h>
  39. typedef long long int num;
  40. num pid;
  41. char tcomm[PATH_MAX];
  42. char state;
  43. num ppid;
  44. num pgid;
  45. num sid;
  46. num tty_nr;
  47. num tty_pgrp;
  48. num flags;
  49. num min_flt;
  50. num cmin_flt;
  51. num maj_flt;
  52. num cmaj_flt;
  53. num utime;
  54. num stimev;
  55. num cutime;
  56. num cstime;
  57. num priority;
  58. num nicev;
  59. num num_threads;
  60. num it_real_value;
  61. unsigned long long start_time;
  62. num vsize;
  63. num rss;
  64. num rsslim;
  65. num start_code;
  66. num end_code;
  67. num start_stack;
  68. num esp;
  69. num eip;
  70. num pending;
  71. num blocked;
  72. num sigign;
  73. num sigcatch;
  74. num wchan;
  75. num zero1;
  76. num zero2;
  77. num exit_signal;
  78. num cpu;
  79. num rt_priority;
  80. num policy;
  81. long tickspersec;
  82. FILE *input;
  83. void readone(num *x) { fscanf(input, "%lld ", x); }
  84. void readunsigned(unsigned long long *x) { fscanf(input, "%llu ", x); }
  85. void readstr(char *x) { fscanf(input, "%s ", x);}
  86. void readchar(char *x) { fscanf(input, "%c ", x);}
  87. void printone(char *name, num x) { printf("%20s: %lld\n", name, x);}
  88. void printonex(char *name, num x) { printf("%20s: %016llx\n", name, x);}
  89. void printunsigned(char *name, unsigned long long x) { printf("%20s: %llu\n", name, x);}
  90. void printchar(char *name, char x) { printf("%20s: %c\n", name, x);}
  91. void printstr(char *name, char *x) { printf("%20s: %s\n", name, x);}
  92. void printtime(char *name, num x) { printf("%20s: %f\n", name, (((double)x) / tickspersec));}
  93. int gettimesinceboot() {
  94. FILE *procuptime;
  95. int sec, ssec;
  96. procuptime = fopen("/proc/uptime", "r");
  97. fscanf(procuptime, "%d.%ds", &sec, &ssec);
  98. fclose(procuptime);
  99. return (sec*tickspersec)+ssec;
  100. }
  101. void printtimediff(char *name, num x) {
  102. int sinceboot = gettimesinceboot();
  103. int running = sinceboot - x;
  104. time_t rt = time(NULL) - (running / tickspersec);
  105. char buf[1024];
  106. strftime(buf, sizeof(buf), "%m.%d %H:%M", localtime(&rt));
  107. printf("%20s: %s (%u.%us)\n", name, buf, running / tickspersec, running % tickspersec);
  108. }
  109. int main(int argc, char *argv[]) {
  110. tickspersec = sysconf(_SC_CLK_TCK);
  111. input = NULL;
  112. if(argc > 1) {
  113. chdir("/proc");
  114. if(chdir(argv[1]) == 0) { input = fopen("stat", "r"); }
  115. if(!input) {
  116. perror("open");
  117. return 1;
  118. }
  119. } else {
  120. input = stdin;
  121. }
  122. readone(&pid);
  123. readstr(tcomm);
  124. readchar(&state);
  125. readone(&ppid);
  126. readone(&pgid);
  127. readone(&sid);
  128. readone(&tty_nr);
  129. readone(&tty_pgrp);
  130. readone(&flags);
  131. readone(&min_flt);
  132. readone(&cmin_flt);
  133. readone(&maj_flt);
  134. readone(&cmaj_flt);
  135. readone(&utime);
  136. readone(&stimev);
  137. readone(&cutime);
  138. readone(&cstime);
  139. readone(&priority);
  140. readone(&nicev);
  141. readone(&num_threads);
  142. readone(&it_real_value);
  143. readunsigned(&start_time);
  144. readone(&vsize);
  145. readone(&rss);
  146. readone(&rsslim);
  147. readone(&start_code);
  148. readone(&end_code);
  149. readone(&start_stack);
  150. readone(&esp);
  151. readone(&eip);
  152. readone(&pending);
  153. readone(&blocked);
  154. readone(&sigign);
  155. readone(&sigcatch);
  156. readone(&wchan);
  157. readone(&zero1);
  158. readone(&zero2);
  159. readone(&exit_signal);
  160. readone(&cpu);
  161. readone(&rt_priority);
  162. readone(&policy);
  163. {
  164. printone("pid", pid);
  165. printstr("tcomm", tcomm);
  166. printchar("state", state);
  167. printone("ppid", ppid);
  168. printone("pgid", pgid);
  169. printone("sid", sid);
  170. printone("tty_nr", tty_nr);
  171. printone("tty_pgrp", tty_pgrp);
  172. printone("flags", flags);
  173. printone("min_flt", min_flt);
  174. printone("cmin_flt", cmin_flt);
  175. printone("maj_flt", maj_flt);
  176. printone("cmaj_flt", cmaj_flt);
  177. printtime("utime", utime);
  178. printtime("stime", stimev);
  179. printtime("cutime", cutime);
  180. printtime("cstime", cstime);
  181. printone("priority", priority);
  182. printone("nice", nicev);
  183. printone("num_threads", num_threads);
  184. printtime("it_real_value", it_real_value);
  185. printtimediff("start_time", start_time);
  186. printone("vsize", vsize);
  187. printone("rss", rss);
  188. printone("rsslim", rsslim);
  189. printone("start_code", start_code);
  190. printone("end_code", end_code);
  191. printone("start_stack", start_stack);
  192. printone("esp", esp);
  193. printone("eip", eip);
  194. printonex("pending", pending);
  195. printonex("blocked", blocked);
  196. printonex("sigign", sigign);
  197. printonex("sigcatch", sigcatch);
  198. printone("wchan", wchan);
  199. printone("zero1", zero1);
  200. printone("zero2", zero2);
  201. printonex("exit_signal", exit_signal);
  202. printone("cpu", cpu);
  203. printone("rt_priority", rt_priority);
  204. printone("policy", policy);
  205. }
  206. return 0;
  207. }