OpenSDN source code
vm_stat_docker.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <sys/times.h>
7 #include <sys/types.h>
8 #include <sys/wait.h>
9 #include <unistd.h>
10 #include <uve/vm_stat_docker.h>
11 #include <uve/vm_stat_data.h>
12 #include <db/db.h>
13 #include <db/db_entry.h>
14 #include <db/db_table.h>
15 #include <base/address.h>
17 #include <cmn/agent.h>
18 #include <uve/vrouter_uve_entry.h>
19 #include <sstream>
20 #include <fstream>
21 #include <uve/agent_uve.h>
22 #include <uve/vm_uve_table.h>
23 
24 using namespace boost::uuids;
25 using namespace boost::asio;
26 
27 VmStatDocker::VmStatDocker(Agent *agent, const uuid &vm_uuid)
28  : VmStat(agent, vm_uuid), container_id_() {
29 }
30 
32 }
33 
35  if (vm_uuid_ == nil_uuid()) {
36  return;
37  }
39 }
40 
42  double cpu_stat = 0;
43  std::string cpu_stat_str;
44  data_ >> cpu_stat_str;
45 
46  if (!cpu_stat_str.empty()) {
47  //Convert string to double
48  std::stringstream ss(cpu_stat_str);
49  ss >> cpu_stat;
50  //Convert from Nanoseconds to seconds
51  cpu_stat = cpu_stat / kOneSecInNanoSecs;
52  }
53 
54  time_t now;
55  time(&now);
57  cpu_usage_ = (cpu_stat - prev_cpu_stat_)/
58  difftime(now, prev_cpu_snapshot_time_);
59  cpu_usage_ *= 100;
60  }
61 
62  prev_cpu_stat_ = cpu_stat;
64 
65  //Clear buffer
66  data_.str(" ");
67  data_.clear();
68 
69  //Trigger a request to start Memory stat collection
71 }
72 
74  if (pid_) {
75  std::ostringstream proc_file;
76  proc_file << "/proc/"<<pid_<<"/status";
77  std::ifstream file(proc_file.str().c_str());
78 
79  bool vmsize = false;
80  bool rss = false;
81  bool peak = false;
82  std::string line;
83  while (std::getline(file, line)) {
84  if (line.find("VmSize") != std::string::npos) {
85  std::stringstream vm(line);
86  std::string tmp; vm >> tmp; vm >> virt_memory_;
87  vmsize = true;
88  }
89  if (line.find("VmRSS") != std::string::npos) {
90  std::stringstream vm(line);
91  std::string tmp;
92  vm >> tmp;
93  vm >> mem_usage_;
94  rss = true;
95  }
96  if (line.find("VmPeak") != std::string::npos) {
97  std::stringstream vm(line);
98  std::string tmp; vm >> tmp; vm >> virt_memory_peak_;
99  peak = true;
100  }
101  if (rss && vmsize && peak)
102  break;
103  }
104  }
105 
106  data_.str(" ");
107  data_.clear();
108 
109  SendVmCpuStats();
110  StartTimer();
111 }
112 
114  std::ostringstream cmd;
115  cmd << "cat /sys/fs/cgroup/cpuacct/docker/" << container_id_
116  << "/cpuacct.usage";
117  ExecCmd(cmd.str(), boost::bind(&VmStatDocker::ReadCpuStat, this));
118 }
119 
121  std::ostringstream cmd;
122  cmd << "cat /sys/fs/cgroup/memory/docker/" << container_id_
123  << "/memory.stat";
124  ExecCmd(cmd.str(), boost::bind(&VmStatDocker::ReadMemoryQuota, this));
125 }
126 
128  ReadMemStat();
129 }
130 
132  std::ostringstream cmd;
133  cmd << "docker inspect " << container_id_ << "| grep -A3 \\\"Paused\\\":";
134  ExecCmd(cmd.str(), boost::bind(&VmStatDocker::ReadPid, this));
135 }
136 
138  /* Expecting data_ to have the following content
139  * "Paused": false,
140  * "Pid": 24430,
141  * "Restarting": false,
142  * "Running": true,
143  */
144  string tmp, paused_str, pid_str, running_str;
145  while (data_ >> tmp) {
146  if (tmp.find("Paused") != std::string::npos) {
147  data_ >> paused_str;
148  }
149  if (tmp.find("Pid") != std::string::npos) {
150  data_ >> pid_str;
151  }
152  if (tmp.find("Running") != std::string::npos) {
153  data_ >> running_str;
154  break;
155  }
156  }
157 
158  //Remove the last character from 'pid_str'
159  if (pid_str.size() >= 2) {
160  pid_str.erase(pid_str.size() - 1);
161  //Convert string to uint32_t
162  std::stringstream ss(pid_str);
163  ss >> pid_;
164  }
165 
166  vm_state_ = VrouterAgentVmState::VROUTER_AGENT_VM_UNKNOWN;
167  if (paused_str == "true,") {
168  vm_state_ = VrouterAgentVmState::VROUTER_AGENT_VM_PAUSED;
169  } else if (running_str == "false,") {
170  vm_state_ = VrouterAgentVmState::VROUTER_AGENT_VM_SHUTDOWN;
171  } else if (running_str == "true,") {
172  vm_state_ = VrouterAgentVmState::VROUTER_AGENT_VM_ACTIVE;
173  }
174  data_.str(" ");
175  data_.clear();
176  GetMemStat();
177 }
178 
180  std::string tmp;
181  while (data_ >> tmp) {
182  if (tmp == "hierarchical_memory_limit") {
184  /* Convert the 'vm_memory_quota_' to KiB to make it consistent with
185  Kvm's 'vm_memory_quota_' */
186  vm_memory_quota_/= 1024;
187  break;
188  }
189  }
190 
191  data_.str(" ");
192  data_.clear();
193  GetPid();
194 }
195 
197  if (container_id_.empty()) {
198  GetContainerId();
199  } else {
200  //Get CPU and memory stats
201  GetCpuStat();
202  }
203  return false;
204 }
205 
207  data_ >> container_id_;
208  //Clear buffer
209  data_.str(" ");
210  data_.clear();
211 
212  if (!container_id_.empty()) {
213  //Successfully read container-id, collect other data
214  GetCpuStat();
215  } else {
216  retry_++;
217  //Retry after timeout
218  if (retry_ < kRetryCount) {
219  StartTimer();
220  }
221  }
222 }
223 
225  std::ostringstream cmd;
226  cmd << "docker ps --no-trunc | grep " << agent_->GetUuidStr(vm_uuid_)
227  << " | awk '{ print $1 }'";
228  ExecCmd(cmd.str(), boost::bind(&VmStatDocker::ReadContainerId, this));
229 }
Definition: agent.h:361
std::string GetUuidStr(boost::uuids::uuid uuid_val) const
Definition: agent.cc:99
void ReadMemoryQuota()
static const long kOneSecInNanoSecs
void GetMemoryQuota()
void ReadMemStat()
void GetContainerId()
void ReadCpuStat()
std::string container_id_
VmStatDocker(Agent *agent, const boost::uuids::uuid &vm_uuid)
void ReadContainerId()
Definition: vm_stat.h:18
uint32_t pid_
Definition: vm_stat.h:61
void SendVmCpuStats()
Definition: vm_stat.cc:180
double prev_cpu_stat_
Definition: vm_stat.h:50
uint32_t virt_memory_
Definition: vm_stat.h:47
VrouterAgentVmState::type vm_state_
Definition: vm_stat.h:67
void StartTimer()
Definition: vm_stat.cc:206
static const uint32_t kRetryCount
Definition: vm_stat.h:21
void ExecCmd(std::string cmd, DoneCb cb)
Definition: vm_stat.cc:75
const boost::uuids::uuid vm_uuid_
Definition: vm_stat.h:45
double cpu_usage_
Definition: vm_stat.h:51
uint32_t retry_
Definition: vm_stat.h:62
time_t prev_cpu_snapshot_time_
Definition: vm_stat.h:52
uint32_t vm_memory_quota_
Definition: vm_stat.h:49
uint32_t virt_memory_peak_
Definition: vm_stat.h:48
uint32_t mem_usage_
Definition: vm_stat.h:46
std::stringstream data_
Definition: vm_stat.h:57
Agent * agent_
Definition: vm_stat.h:44
boost::uuids::uuid uuid