Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mem-info.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "util/mem-info.h"
16 #include "util/debug-util.h"
17 #include "util/string-parser.h"
18 
19 #include <boost/algorithm/string.hpp>
20 #include <boost/lexical_cast.hpp>
21 #include <iostream>
22 #include <fstream>
23 #include <sstream>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include "util/pretty-printer.h"
29 
30 #include "common/names.h"
31 
32 using boost::algorithm::is_any_of;
33 using boost::algorithm::split;
34 using boost::algorithm::token_compress_on;
35 
36 namespace impala {
37 
38 bool MemInfo::initialized_ = false;
39 int64_t MemInfo::physical_mem_ = -1;
40 int32_t MemInfo::vm_overcommit_ = -1;
41 int64_t MemInfo::commit_limit_ = -1;
42 
43 // Lines in meminfo have the form key colon whitespace value for example:
44 // MemTotal: 16129508 kB
45 int64_t ParseMemString(const char* val, size_t len) {
47  int64_t mem_total_kb = StringParser::StringToInt<int64_t>(val,
48  len, &result);
49  if (result != StringParser::PARSE_SUCCESS) return -1;
50  // Entries in /proc/meminfo are in KB.
51  return mem_total_kb * 1024L;
52 }
53 
54 void MemInfo::Init() {
55  // Read overcommit settings
57 
58  // Read from /proc/meminfo
59  ifstream meminfo("/proc/meminfo", ios::in);
60  string line;
61  while (meminfo.good() && !meminfo.eof()) {
62  getline(meminfo, line);
63  vector<string> fields;
64  split(fields, line, is_any_of(" "), token_compress_on);
65  // We expect lines such as, e.g., 'MemTotal: 16129508 kB'
66  if (fields.size() < 3) continue;
67 
68  // Make sure that the format of the file does not change
69  DCHECK_EQ(fields[2], "kB");
70 
71  if (fields[0].compare("MemTotal:") == 0) {
72  physical_mem_ = ParseMemString(fields[1].data(), fields[1].size());
73  } else if (fields[0].compare("CommitLimit:") == 0) {
74  commit_limit_ = ParseMemString(fields[1].data(), fields[1].size());
75  }
76  }
77  if (meminfo.is_open()) meminfo.close();
78 
79  if (physical_mem_ == -1) {
80  LOG(WARNING) << "Could not determine amount of physical memory on this machine "
81  << "using /proc/meminfo.";
82  }
83 
84  if (commit_limit_ == -1) {
85  LOG(WARNING) << "Could not determine memory commit limit on this machine "
86  << "using /proc/meminfo.";
87  }
88  initialized_ = true;
89 }
90 
92  ifstream overcommit_s("/proc/sys/vm/overcommit_memory", ios::in);
93  overcommit_s >> vm_overcommit_;
94 }
95 
97  DCHECK(initialized_);
98  stringstream stream;
99  stream << "Physical Memory: "
100  << PrettyPrinter::Print(physical_mem_, TUnit::BYTES)
101  << endl;
102  return stream.str();
103 }
104 
105 }
int64_t ParseMemString(const char *val, size_t len)
Definition: mem-info.cc:45
static int32_t vm_overcommit_
Indicating the kernel overcommit settings.
Definition: mem-info.h:70
static void Init()
Initialize MemInfo.
Definition: mem-info.cc:54
static int64_t physical_mem_
Definition: mem-info.h:61
static std::string Print(bool value, TUnit::type ignored, bool verbose=false)
static std::string DebugString()
Definition: mem-info.cc:96
static void ParseOvercommit()
Definition: mem-info.cc:91
static bool initialized_
Definition: mem-info.h:60
static int64_t commit_limit_
If overcommit is turned off the maximum allocatable memory.
Definition: mem-info.h:73