Guest User

Untitled

a guest
Apr 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. FILE* status = fopen( "/proc/self/status", "r" );
  2.  
  3. long ru_idrss; /* integral unshared data size */
  4. long ru_isrss; /* integral unshared stack size */
  5.  
  6. #include <sys/resource.h>
  7. #include <errno.h>
  8.  
  9. errno = 0;
  10. struct rusage* memory = malloc(sizeof(struct rusage));
  11. getrusage(RUSAGE_SELF, memory);
  12. if(errno == EFAULT)
  13. printf("Error: EFAULTn");
  14. else if(errno == EINVAL)
  15. printf("Error: EINVALn");
  16. printf("Usage: %ldn", memory->ru_ixrss);
  17. printf("Usage: %ldn", memory->ru_isrss);
  18. printf("Usage: %ldn", memory->ru_idrss);
  19. printf("Max: %ldn", memory->ru_maxrss);
  20.  
  21. typedef struct {
  22. unsigned long size,resident,share,text,lib,data,dt;
  23. } statm_t;
  24.  
  25. void read_off_memory_status(statm_t& result)
  26. {
  27. unsigned long dummy;
  28. char statm_path[100];
  29. snprintf(statm_path,99,"/proc/%d/statm",PID);
  30.  
  31. FILE *f = fopen(statm_path,"r");
  32. if(!f){
  33. perror(statm_path);
  34. abort();
  35. }
  36. if(7 != fscanf(f,"%ld %ld %ld %ld %ld %ld %ld",
  37. &result.size,&result.resident,&result.share,&result.text,&result.lib,&result.data,&result.dt))
  38. {
  39. perror(statm_path);
  40. abort();
  41. }
  42. fclose(f);
  43. }
  44.  
  45. /proc/[pid]/statm
  46. Provides information about memory usage, measured in pages.
  47. The columns are:
  48.  
  49. size total program size
  50. (same as VmSize in /proc/[pid]/status)
  51. resident resident set size
  52. (same as VmRSS in /proc/[pid]/status)
  53. share shared pages (from shared mappings)
  54. text text (code)
  55. lib library (unused in Linux 2.6)
  56. data data + stack
  57. dt dirty pages (unused in Linux 2.6)
Add Comment
Please, Sign In to add comment