NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
mem-usage.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #ifndef MEM_USAGE_H
21 #define MEM_USAGE_H
22 
23 #ifdef __linux__
24 // #include <proc/readproc.h>
25 // #include <unistd.h>
26 // // #include <sys/resource.h>
27 #include <sys/sysinfo.h>
28 #endif
29 
30 #ifdef __APPLE__
31 #include <mach/task.h>
32 #include <mach/mach_traps.h>
33 #include <mach/mach.h>
34 #include <unistd.h>
35 #include <err.h>
36 #include <sys/param.h>
37 #include <mach-o/ldsyms.h>
38 #endif
39 
44 class MemUsage {
45 public:
49  static inline int64_t
50  Get()
51  {
52 #if defined(__linux__)
53  /*
54  /proc/[pid]/statm
55  Provides information about memory usage, measured in pages. The
56  columns are:
57 
58  size (1) total program size
59  (same as VmSize in /proc/[pid]/status)
60  resident (2) resident set size
61  (same as VmRSS in /proc/[pid]/status)
62  share (3) shared pages (i.e., backed by a file)
63  text (4) text (code)
64  lib (5) library (unused in Linux 2.6)
65  data (6) data + stack
66  dt (7) dirty pages (unused in Linux 2.6)
67 
68  Reference: http://man7.org/linux/man-pages/man5/proc.5.html
69  */
70  std::ifstream is("/proc/self/statm");
71  if (!is.bad() && !is.eof()) {
72  unsigned long vm;
73  unsigned long rss;
74  is >> vm // the first number: virtual memory
75  >> rss; // the second number: resident set size
76 
77  return rss * getpagesize();
78  }
79  else {
80  return -1;
81  }
82 
83 #elif defined(__APPLE__)
84  struct task_basic_info t_info;
85  mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
86 
87  if (KERN_SUCCESS
88  != task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count)) {
89  return -1; // something is wrong
90  }
91 
92  return t_info.resident_size;
93 #endif
94  // other systems are not yet supported
95  return -1;
96  }
97 };
98 
99 #endif // MEM_USAGE_H
static int64_t Get()
Get memory utilization in bytes.
Definition: mem-usage.hpp:50
Copyright (c) 2011-2015 Regents of the University of California.
Definition: mem-usage.hpp:44