OpenSDN source code
vm_stat_kvm.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 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_kvm.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 VmStatKvm::VmStatKvm(Agent *agent, const uuid &vm_uuid)
28  : VmStat(agent, vm_uuid) {
29 }
30 
32 }
33 
35  std::string tmp;
36  //Typical output from command
37  //Id: 1
38  //Name: instance-00000001
39  //UUID: 90cb7351-d2dc-4d8d-a216-2f460be183b6
40  //OS Type: hvm
41  //State: running
42  //CPU(s): 1
43  //CPU time: 13.4s
44  //Max memory: 2097152 KiB
45 
46  //Get 'CPU time' from the output
47  double cpu_stat = 0;
48  std::string cpu_stat_str, state_str, cpu_count_str;
49 
50  while (data_ >> tmp) {
51  if (tmp == "State:") {
52  data_ >> state_str;
53  }
54  if (tmp == "CPU(s):") {
55  data_ >> cpu_count_str;
56  }
57  if (tmp == "time:") {
58  data_ >> cpu_stat_str;
59  /* We expect 'State' and 'CPU(s)' fields to be present before
60  * 'CPU time' field. So break from the loop when we are done with
61  * reading of 'CPU time' field. */
62  break;
63  }
64  }
65 
66  vm_state_ = VrouterAgentVmState::VROUTER_AGENT_VM_UNKNOWN;
67  if (state_str.size()) {
68  if (state_str == "running") {
69  vm_state_ = VrouterAgentVmState::VROUTER_AGENT_VM_ACTIVE;
70  } else if (state_str == "paused") {
71  vm_state_ = VrouterAgentVmState::VROUTER_AGENT_VM_PAUSED;
72  } else if (state_str == "shut") {
73  vm_state_ = VrouterAgentVmState::VROUTER_AGENT_VM_SHUTDOWN;
74  }
75  }
77  if (cpu_count_str.size()) {
78  stringToInteger(cpu_count_str, vm_cpu_count_);
79  }
80  //Remove the last character from 'cpu_stat_str'
81  if (cpu_stat_str.size() >= 2) {
82  cpu_stat_str.erase(cpu_stat_str.size() - 1);
83  //Convert string to double
84  std::stringstream ss(cpu_stat_str);
85  ss >> cpu_stat;
86  }
87 
88  time_t now;
89  time(&now);
91  cpu_usage_ = (cpu_stat - prev_cpu_stat_)/
92  difftime(now, prev_cpu_snapshot_time_);
93  cpu_usage_ *= 100;
94  }
95 
96  prev_cpu_stat_ = cpu_stat;
98 
99  //Clear buffer
100  data_.str(" ");
101  data_.clear();
102 
103  //Trigger a request to start vcpu stat collection
104  GetVcpuStat();
105 }
106 
108  std::string tmp;
109  uint32_t index = 0;
110  std::vector<double> vcpu_usage;
111  //Read latest VCPU usage time
112  while(data_ >> tmp) {
113  if (tmp == "VCPU:") {
114  //Current VCPU index
115  data_ >> index;
116  }
117 
118  if (tmp == "time:") {
119  double usage = 0;
120  data_ >> usage;
121  vcpu_usage.push_back(usage);
122  }
123  }
124 
125  vcpu_usage_percent_.clear();
126  if (prev_vcpu_usage_.size() != vcpu_usage.size()) {
127  //In case a new VCPU get added
128  prev_vcpu_usage_ = vcpu_usage;
129  }
130 
131  time_t now;
132  time(&now);
133  //Calculate VCPU usage
135  for (uint32_t i = 0; i < vcpu_usage.size(); i++) {
136  double cpu_usage = (vcpu_usage[i] - prev_vcpu_usage_[i])/
137  difftime(now, prev_vcpu_snapshot_time_);
138  cpu_usage *= 100;
139  vcpu_usage_percent_.push_back(cpu_usage);
140  }
141  }
142 
143  prev_vcpu_usage_ = vcpu_usage;
145 
146  data_.str(" ");
147  data_.clear();
148  //Trigger a request to start mem stat
149  GetMemStat();
150 }
151 
153  if (pid_) {
154  std::ostringstream proc_file;
155  proc_file << "/proc/"<<pid_<<"/status";
156  std::ifstream file(proc_file.str().c_str());
157 
158  bool vmsize = false;
159  bool peak = false;
160  bool rss = false;
161  std::string line;
162  while (std::getline(file, line)) {
163  if (line.find("VmSize") != std::string::npos) {
164  std::stringstream vm(line);
165  std::string tmp; vm >> tmp; vm >> virt_memory_;
166  vmsize = true;
167  }
168  if (line.find("VmRSS") != std::string::npos) {
169  std::stringstream vm(line);
170  std::string tmp;
171  vm >> tmp;
172  vm >> mem_usage_;
173  rss = true;
174  }
175  if (line.find("VmPeak") != std::string::npos) {
176  std::stringstream vm(line);
177  std::string tmp; vm >> tmp; vm >> virt_memory_peak_;
178  peak = true;
179  }
180  if (rss && vmsize && peak)
181  break;
182  }
183  }
184 
185  data_.str(" ");
186  data_.clear();
187  GetDiskName();
188 }
189 
191  std::ostringstream cmd;
192  cmd << "virsh dominfo " << agent_->GetUuidStr(vm_uuid_);
193  ExecCmd(cmd.str(), boost::bind(&VmStatKvm::ReadCpuStat, this));
194 }
195 
197  std::ostringstream cmd;
198  cmd << "virsh vcpuinfo " << agent_->GetUuidStr(vm_uuid_);
199  ExecCmd(cmd.str(), boost::bind(&VmStatKvm::ReadVcpuStat, this));
200 }
201 
203  ReadMemStat();
204 }
205 
207  std::ostringstream cmd;
208  cmd << "virsh domblklist " << agent_->GetUuidStr(vm_uuid_) << " | grep "
210  ExecCmd(cmd.str(), boost::bind(&VmStatKvm::ReadDiskName, this));
211 }
212 
214  data_ >> disk_name_;
215  if (!disk_name_.empty()) {
216  GetDiskStat();
217  } else {
218  SendVmCpuStats();
219  StartTimer();
220  }
221 }
222 
224  std::ostringstream cmd;
225  cmd << "virsh domblkinfo " << agent_->GetUuidStr(vm_uuid_) << " "
226  << disk_name_;
227  ExecCmd(cmd.str(), boost::bind(&VmStatKvm::ReadDiskStat, this));
228 }
229 
231  bool disk_size_found = false, virtual_size_found = false;
232  std::string tmp;
233  std::string virtual_size_str, disk_size_str;
234  while (data_ >> tmp) {
235  if (tmp == "Capacity:") {
236  data_ >> virtual_size_str;
237  virtual_size_found = true;
238  } else if (tmp == "Allocation:") {
239  data_ >> disk_size_str;
240  disk_size_found = true;
241  }
242  if (virtual_size_found && disk_size_found) {
243  break;
244  }
245  }
246  if (virtual_size_str.size() >= 2) {
247  //Convert string to uint32_t
248  std::stringstream ss(virtual_size_str);
249  ss >> virtual_size_;
250  }
251 
252  if (disk_size_str.size() >= 2) {
253  //Convert string to uint32_t
254  std::stringstream ss(disk_size_str);
255  ss >> disk_size_;
256  }
257 
258  SendVmCpuStats();
259  StartTimer();
260 }
261 
263  std::string tmp;
264  while (data_ >> tmp) {
265  if (tmp == "actual") {
267  }
268  }
269  GetCpuStat();
270 }
271 
273  std::ostringstream cmd;
274  cmd << "virsh dommemstat " << agent_->GetUuidStr(vm_uuid_);
275  ExecCmd(cmd.str(), boost::bind(&VmStatKvm::ReadMemoryQuota, this));
276 }
277 
279  if (pid_ == 0) {
280  GetPid();
281  } else {
282  //Get CPU and memory stats
283  GetCpuStat();
284  }
285  return false;
286 }
287 
289  std::string tmp;
290  uint32_t pid;
291 
292  while (data_) {
293  data_ >> pid;
294  data_ >> tmp;
295  if (tmp.find("qemu") != std::string::npos ||
296  tmp.find("kvm") != std::string::npos) {
297  //Copy PID
298  pid_ = pid;
299  break;
300  }
301  //Flush out this line
302  data_.ignore(512, '\n');
303  }
304 
305  data_.str(" ");
306  data_.clear();
307  if (pid_) {
308  //Successfully read pid of process, collect other data
309  GetMemoryQuota();
310  } else {
311  retry_++;
312  //Retry after timeout
313  if (retry_ < kRetryCount) {
314  StartTimer();
315  }
316  }
317 }
318 
320  std::ostringstream cmd;
321  cmd << "ps -eo pid,cmd | grep " << agent_->GetUuidStr(vm_uuid_)
322  << " | grep instance-";
323  ExecCmd(cmd.str(), boost::bind(&VmStatKvm::ReadPid, this));
324 }
325 
327  GetPid();
328 }
Definition: agent.h:361
std::string GetUuidStr(boost::uuids::uuid uuid_val) const
Definition: agent.cc:99
void ReadDiskStat()
Definition: vm_stat_kvm.cc:230
void GetDiskStat()
Definition: vm_stat_kvm.cc:223
void ReadMemStat()
Definition: vm_stat_kvm.cc:152
void GetDiskName()
Definition: vm_stat_kvm.cc:206
void ReadMemoryQuota()
Definition: vm_stat_kvm.cc:262
void Start()
Definition: vm_stat_kvm.cc:326
void GetCpuStat()
Definition: vm_stat_kvm.cc:190
void GetVcpuStat()
Definition: vm_stat_kvm.cc:196
void ReadPid()
Definition: vm_stat_kvm.cc:288
void ReadCpuStat()
Definition: vm_stat_kvm.cc:34
void ReadDiskName()
Definition: vm_stat_kvm.cc:213
bool TimerExpiry()
Definition: vm_stat_kvm.cc:278
void GetPid()
Definition: vm_stat_kvm.cc:319
void ReadVcpuStat()
Definition: vm_stat_kvm.cc:107
VmStatKvm(Agent *agent, const boost::uuids::uuid &vm_uuid)
Definition: vm_stat_kvm.cc:27
void GetMemoryQuota()
Definition: vm_stat_kvm.cc:272
void GetMemStat()
Definition: vm_stat_kvm.cc:202
Definition: vm_stat.h:18
uint32_t disk_size_
Definition: vm_stat.h:65
uint32_t pid_
Definition: vm_stat.h:61
void SendVmCpuStats()
Definition: vm_stat.cc:180
double prev_cpu_stat_
Definition: vm_stat.h:50
std::vector< double > vcpu_usage_percent_
Definition: vm_stat.h:54
uint32_t virt_memory_
Definition: vm_stat.h:47
uint32_t virtual_size_
Definition: vm_stat.h:64
VrouterAgentVmState::type vm_state_
Definition: vm_stat.h:67
void StartTimer()
Definition: vm_stat.cc:206
uint16_t vm_cpu_count_
Definition: vm_stat.h:69
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
static const uint16_t kInvalidCpuCount
Definition: vm_stat.h:22
uint32_t retry_
Definition: vm_stat.h:62
std::string disk_name_
Definition: vm_stat.h:66
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
std::vector< double > prev_vcpu_usage_
Definition: vm_stat.h:53
uint32_t mem_usage_
Definition: vm_stat.h:46
time_t prev_vcpu_snapshot_time_
Definition: vm_stat.h:55
std::stringstream data_
Definition: vm_stat.h:57
Agent * agent_
Definition: vm_stat.h:44
bool stringToInteger(const std::string &str, NumberType &num)
Definition: string_util.h:72
boost::uuids::uuid uuid