GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
memory_info.cpp
1 /**
2  * Copyright (c) 2009 Carnegie Mellon University.
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an "AS
13  * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14  * express or implied. See the License for the specific language
15  * governing permissions and limitations under the License.
16  *
17  * For more about this software visit:
18  *
19  * http://www.graphlab.ml.cmu.edu
20  *
21  */
22 
23 #include <iostream>
24 
25 #ifdef HAS_TCMALLOC
26 #include <google/malloc_extension.h>
27 #endif
28 #include <graphlab/logger/assertions.hpp>
29 
30 namespace graphlab {
31  namespace memory_info {
32 
33  bool available() {
34 #ifdef HAS_TCMALLOC
35  return true;
36 #else
37  return false;
38 #endif
39  } // end of available
40 
41 
42 
43  size_t heap_bytes() {
44  size_t heap_size(0);
45 #ifdef HAS_TCMALLOC
46  MallocExtension::instance()->
47  GetNumericProperty("generic.heap_size", &heap_size);
48 #else
49  logstream(LOG_WARNING) <<
50  "memory_info::heap_bytes() requires tcmalloc" << std::endl;
51 #endif
52  return heap_size;
53  } // end of heap size
54 
55 
56 
57  size_t allocated_bytes() {
58  size_t allocated_size(0);
59 #ifdef HAS_TCMALLOC
60  MallocExtension::instance()->
61  GetNumericProperty("generic.current_allocated_bytes",
62  &allocated_size);
63 #else
64  logstream_once(LOG_WARNING) <<
65  "memory_info::allocated_bytes() requires tcmalloc" << std::endl;
66 #endif
67  return allocated_size;
68  } // end of allocated bytes
69 
70 
71 
72  void print_usage(const std::string& label) {
73 #ifdef HAS_TCMALLOC
74  const double BYTES_TO_MB = double(1) / double(1024 * 1024);
75  std::cout
76  << "Memory Info: " << label << std::endl
77  << "\t Heap: " << (heap_bytes() * BYTES_TO_MB) << " MB"
78  << std::endl
79  << "\t Allocated: " << (allocated_bytes() * BYTES_TO_MB) << " MB"
80  << std::endl;
81 #else
82  logstream_once(LOG_WARNING)
83  << "Unable to print memory info for: " << label << ". "
84  << "No memory extensions api available." << std::endl;
85 #endif
86  } // end of print_usage
87 
88  void log_usage(const std::string& label) {
89 #ifdef HAS_TCMALLOC
90  const double BYTES_TO_MB = double(1) / double(1024 * 1024);
91  logstream(LOG_INFO)
92  << "Memory Info: " << label
93  << "\n\t Heap: " << (heap_bytes() * BYTES_TO_MB) << " MB"
94  << "\n\t Allocated: " << (allocated_bytes() * BYTES_TO_MB) << " MB"
95  << std::endl;
96 #else
97  logstream_once(LOG_WARNING)
98  << "Unable to print memory info for: " << label << ". "
99  << "No memory extensions api available." << std::endl;
100 #endif
101  } // end of log usage
102 
103 
104  }; // end of namespace memory info
105 
106 }; // end of graphlab namespace
107 
108