Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #ifndef MEM_USAGE_H |
| 22 | #define MEM_USAGE_H |
| 23 | |
Saran Tarnoi | 9b56987 | 2013-03-28 14:26:11 -0700 | [diff] [blame] | 24 | #ifdef __linux__ |
Alexander Afanasyev | ef91d29 | 2013-02-20 18:58:40 -0800 | [diff] [blame] | 25 | // #include <proc/readproc.h> |
| 26 | // #include <unistd.h> |
| 27 | // // #include <sys/resource.h> |
Saran Tarnoi | 9b56987 | 2013-03-28 14:26:11 -0700 | [diff] [blame] | 28 | #include <sys/sysinfo.h> |
| 29 | #endif |
| 30 | |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 31 | #ifdef __APPLE__ |
| 32 | #include <mach/task.h> |
| 33 | #include <mach/mach_traps.h> |
| 34 | #include <mach/mach.h> |
| 35 | #include <unistd.h> |
| 36 | #include <err.h> |
| 37 | #include <sys/param.h> |
| 38 | #include <mach-o/ldsyms.h> |
| 39 | #endif |
| 40 | |
| 41 | /** |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 42 | * @ingroup ndn-helpers |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 43 | * @brief Utility class to evaluate current usage of RAM |
| 44 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 45 | class MemUsage { |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 46 | public: |
| 47 | /** |
| 48 | * @brief Get memory utilization in bytes |
| 49 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 50 | static inline int64_t |
| 51 | Get() |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 52 | { |
Saran Tarnoi | 9b56987 | 2013-03-28 14:26:11 -0700 | [diff] [blame] | 53 | #if defined(__linux__) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 54 | /* |
| 55 | /proc/[pid]/statm |
| 56 | Provides information about memory usage, measured in pages. The |
| 57 | columns are: |
Saran Tarnoi | 9b56987 | 2013-03-28 14:26:11 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 59 | size (1) total program size |
| 60 | (same as VmSize in /proc/[pid]/status) |
| 61 | resident (2) resident set size |
| 62 | (same as VmRSS in /proc/[pid]/status) |
| 63 | share (3) shared pages (i.e., backed by a file) |
| 64 | text (4) text (code) |
| 65 | lib (5) library (unused in Linux 2.6) |
| 66 | data (6) data + stack |
| 67 | dt (7) dirty pages (unused in Linux 2.6) |
Saran Tarnoi | 9b56987 | 2013-03-28 14:26:11 -0700 | [diff] [blame] | 68 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 69 | Reference: http://man7.org/linux/man-pages/man5/proc.5.html |
| 70 | */ |
| 71 | std::ifstream is("/proc/self/statm"); |
| 72 | if (!is.bad() && !is.eof()) { |
| 73 | unsigned long vm; |
| 74 | unsigned long rss; |
| 75 | is >> vm // the first number: virtual memory |
| 76 | >> rss; // the second number: resident set size |
| 77 | |
| 78 | return rss * getpagesize(); |
| 79 | } |
| 80 | else { |
| 81 | return -1; |
| 82 | } |
Saran Tarnoi | 9b56987 | 2013-03-28 14:26:11 -0700 | [diff] [blame] | 83 | |
| 84 | #elif defined(__APPLE__) |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 85 | struct task_basic_info t_info; |
| 86 | mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; |
| 87 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | if (KERN_SUCCESS |
| 89 | != task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count)) { |
| 90 | return -1; // something is wrong |
| 91 | } |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 92 | |
| 93 | return t_info.resident_size; |
| 94 | #endif |
| 95 | // other systems are not yet supported |
| 96 | return -1; |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | #endif // MEM_USAGE_H |