Guest User

Untitled

a guest
Feb 18th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <time.h>
  7.  
  8. #define ARG_CPU "ucp"
  9. #define ARG_CPU_MEM "ucp-mem"
  10.  
  11. #define DEFAULT_TIME_TRACKING 10
  12. #define DEFAULT_MEM_ALLOCATION 1
  13.  
  14. #define CMD_CPU_USAGE_FORMAT "ps u %d | awk '{print $3}' | grep -v %%"
  15. #define CMD_MEM_USAGE_FORMAT "ps u %d | awk '{print $5}' | grep -v VSZ"
  16. #define CMD_KILL_PROCESS_FORMAT "kill %d"
  17.  
  18. void display_process_resources_usage(int pid, int display_mem)
  19. {
  20. char bash_cmd[256];
  21. sprintf(bash_cmd, CMD_CPU_USAGE_FORMAT, pid);
  22.  
  23. FILE* pipe = popen(bash_cmd, "r");
  24.  
  25. if (pipe == NULL) {
  26. printf("Error!");
  27. exit(-1);
  28. }
  29.  
  30. char buffer[64];
  31. char* cpu_usage = fgets(buffer, sizeof(buffer), pipe);
  32. printf("CPU: %% %s", cpu_usage);
  33.  
  34. if (display_mem) {
  35. sprintf(bash_cmd, CMD_MEM_USAGE_FORMAT, pid);
  36. pipe = popen(bash_cmd, "r");
  37.  
  38. char* mem_usage = fgets(buffer, sizeof(buffer), pipe);
  39. printf("MEM: KB %s", mem_usage);
  40. }
  41. pclose(pipe);
  42. }
  43.  
  44. void track_process_resources_usage(int pid, int time_in_seconds, int track_memory)
  45. {
  46. time_t start, end;
  47. double elapsed = 0.0;
  48. int elapsed_aux = -1;
  49. struct tm* time_info;
  50.  
  51. time(&start);
  52.  
  53. while (elapsed < time_in_seconds) {
  54. time(&end);
  55. elapsed = difftime(end, start);
  56.  
  57. if (elapsed_aux == (int) elapsed) continue;
  58.  
  59. elapsed_aux = elapsed;
  60. time_info = localtime(&end);
  61.  
  62. printf("\n%s", asctime(time_info));
  63. display_process_resources_usage(pid, track_memory);
  64. }
  65. printf("\n");
  66.  
  67. char cmd_kill_process[32];
  68. sprintf(cmd_kill_process, CMD_KILL_PROCESS_FORMAT, pid);
  69. system(cmd_kill_process);
  70. }
  71.  
  72. void consume_cpu()
  73. {
  74. while (1) {}
  75. }
  76.  
  77. void consume_cpu_and_memory()
  78. {
  79. while (1) {
  80. malloc(DEFAULT_MEM_ALLOCATION);
  81. }
  82. }
  83.  
  84. int main (int argc, char *argv[])
  85. {
  86. if (argv[1] == NULL || (strcmp(argv[1], ARG_CPU) != 0 && strcmp(argv[1], ARG_CPU_MEM) != 0)) {
  87. printf("\nUse %s or %s as argument.\n\n", ARG_CPU, ARG_CPU_MEM);
  88. return 0;
  89. }
  90.  
  91. int pid = fork();
  92.  
  93. if (pid < 0) {
  94. printf("An error occurred during fork process!");
  95. exit(-1);
  96. }
  97. else if (pid > 0) {
  98. if (strcmp(argv[1], ARG_CPU) == 0) {
  99. track_process_resources_usage(pid, DEFAULT_TIME_TRACKING, 0);
  100. }
  101. else if (strcmp(argv[1], ARG_CPU_MEM) == 0) {
  102. track_process_resources_usage(pid, DEFAULT_TIME_TRACKING, 1);
  103. }
  104. }
  105. else {
  106. if (strcmp(argv[1], ARG_CPU) == 0) {
  107. consume_cpu();
  108. }
  109. else if (strcmp(argv[1], ARG_CPU_MEM) == 0) {
  110. consume_cpu_and_memory();
  111. }
  112. }
  113. return 0;
  114. }
Add Comment
Please, Sign In to add comment