OpenSDN source code
vm_stat.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.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 <init/agent_param.h>
19 #include <uve/vrouter_uve_entry.h>
20 #include <sstream>
21 #include <fstream>
22 #include <uve/agent_uve.h>
23 #include <uve/vm_uve_table.h>
24 
25 using namespace boost::uuids;
26 using namespace boost::asio;
27 
28 VmStat::VmStat(Agent *agent, const uuid &vm_uuid):
29  agent_(agent), vm_uuid_(vm_uuid), mem_usage_(0),
30  virt_memory_(0), virt_memory_peak_(0), vm_memory_quota_(0),
31  prev_cpu_stat_(0), cpu_usage_(0),
32  prev_cpu_snapshot_time_(0), prev_vcpu_snapshot_time_(0),
33  input_(*(agent_->event_manager()->io_service())),
34  timer_(TimerManager::CreateTimer(*(agent_->event_manager())->io_service(),
35  "VmStatTimer")), marked_delete_(false), pid_(0), retry_(0), virtual_size_(0),
36  disk_size_(0), disk_name_(),
37  vm_state_(VrouterAgentVmState::VROUTER_AGENT_VM_UNKNOWN),
38  prev_vm_state_(VrouterAgentVmState::VROUTER_AGENT_VM_UNKNOWN),
39  vm_cpu_count_(kInvalidCpuCount), prev_vm_cpu_count_(kInvalidCpuCount) {
40 }
41 
44 }
45 
46 void VmStat::ReadData(const boost::system::error_code &ec,
47  size_t read_bytes, DoneCb &cb) {
48  if (read_bytes) {
49  data_<< rx_buff_;
50  }
51 
52  if (ec) {
53  boost::system::error_code close_ec;
54  input_.close(close_ec);
55  call_back_ = cb;
56  //Enqueue a request to process data
57  VmStatData *vm_stat_data = new VmStatData(this);
58 
59  VmUveTable *vmt = static_cast<VmUveTable *>
60  (agent_->uve()->vm_uve_table());
61  vmt->EnqueueVmStatData(vm_stat_data);
62  } else {
63  bzero(rx_buff_, sizeof(rx_buff_));
64  async_read(input_, boost::asio::buffer(rx_buff_, kBufLen),
65  boost::bind(&VmStat::ReadData, this, placeholders::error,
66  placeholders::bytes_transferred, cb));
67  }
68 }
69 
71  if (!call_back_.empty())
72  call_back_();
73 }
74 
75 void VmStat::ExecCmd(std::string cmd, DoneCb cb) {
76  char *argv[4];
77  char shell[80] = "/bin/sh";
78  char option[80] = "-c";
79  char ccmd[256];
80  memset(ccmd, 0, sizeof(ccmd));
81  strncpy(ccmd, cmd.c_str(), sizeof(ccmd)-1);
82 
83  argv[0] = shell;
84  argv[1] = option;
85  argv[2] = ccmd;
86  argv[3] = 0;
87 
88  int out[2];
89  if (pipe(out) < 0) {
90  return;
91  }
92 
93  if (vfork() == 0) {
94  //Close read end of pipe
95  close(out[0]);
96  dup2(out[1], STDOUT_FILENO);
97  //Close out[1] as stdout is a exact replica of out[1]
98  close(out[1]);
99 
100  /* Close all the open fds before execvp */
101  CloseTaskFds();
102  execvp(argv[0], argv);
103  perror("execvp");
104  exit(127);
105  }
106 
107  //Close write end of pipe
108  close(out[1]);
109 
110  boost::system::error_code ec;
111  int fd = ::dup(out[0]);
112  close(out[0]);
113  if (fd == -1) {
114  return;
115  }
116  input_.assign(fd, ec);
117  if (ec) {
118  close(fd);
119  return;
120  }
121 
122  bzero(rx_buff_, sizeof(rx_buff_));
123  async_read(input_, boost::asio::buffer(rx_buff_, kBufLen),
124  boost::bind(&VmStat::ReadData, this, placeholders::error,
125  placeholders::bytes_transferred, cb));
126 }
127 
128 bool VmStat::BuildVmStatsMsg(VirtualMachineStats *uve) {
129  uve->set_name(UuidToString(vm_uuid_));
130 
131  std::vector<VmCpuStats> cpu_stats_list;
132  VmCpuStats stats;
133  stats.set_cpu_one_min_avg(cpu_usage_);
134  stats.set_vm_memory_quota(vm_memory_quota_);
135  stats.set_rss(mem_usage_);
136  stats.set_virt_memory(virt_memory_);
137  stats.set_peak_virt_memory(virt_memory_peak_);
138  stats.set_disk_allocated_bytes(virtual_size_);
139  stats.set_disk_used_bytes(disk_size_);
140 
141 
142  cpu_stats_list.push_back(stats);
143  uve->set_cpu_stats(cpu_stats_list);
144 
145  return true;
146 }
147 
148 bool VmStat::BuildVmMsg(UveVirtualMachineAgent *uve) {
149  uve->set_name(UuidToString(vm_uuid_));
150 
151  VmCpuStats stats;
152  stats.set_cpu_one_min_avg(cpu_usage_);
153  stats.set_vm_memory_quota(vm_memory_quota_);
154  stats.set_rss(mem_usage_);
155  stats.set_virt_memory(virt_memory_);
156  stats.set_peak_virt_memory(virt_memory_peak_);
157  stats.set_disk_allocated_bytes(virtual_size_);
158  stats.set_disk_used_bytes(disk_size_);
159 
160  uve->set_cpu_info(stats);
161 
162  vnsConstants vns;
163  if (vm_state_ != VrouterAgentVmState::VROUTER_AGENT_VM_UNKNOWN) {
164  if (vm_state_ != prev_vm_state_) {
165  uve->set_vm_state(vns.VrouterAgentVmStateMap.at(vm_state_));
167  }
168  }
169 
172  uve->set_vm_cpu_count(vm_cpu_count_);
174  }
175  }
176 
177  return true;
178 }
179 
181  //We need to send same cpu info in two different UVEs
182  //(VirtualMachineStats and UveVirtualMachineAgent). One of them uses
183  //stats-oracle infra and other one does not use it. We need two because
184  //stats-oracle infra returns only SUM of cpu-info over a period of time
185  //and current value is returned using non-stats-oracle version. Using
186  //stats oracle infra we can still query the current value but for simpler
187  //interface we are sending current value in separate UVE.
188  //Also the non-stats oracle version has additional fields of vm_state and
189  //vm_cpu_count which are not sent in stats-oracle version.
190  VirtualMachineStats vm_agent;
191  if (BuildVmStatsMsg(&vm_agent)) {
192  VmUveTable *vmt = static_cast<VmUveTable *>
193  (agent_->uve()->vm_uve_table());
194  vmt->DispatchVmStatsMsg(vm_agent);
195  }
196  UveVirtualMachineAgent vm_msg;
197  if (BuildVmMsg(&vm_msg)) {
198  agent_->uve()->vm_uve_table()->DispatchVmMsg(vm_msg);
199  }
200 }
201 
203  return false;
204 }
205 
207  timer_->Cancel();
209  boost::bind(&VmStat::TimerExpiry, this));
210 }
211 
213 }
214 
215 void VmStat::Stop() {
216  marked_delete_ = true;
217  if (timer_->running() || retry_ == kRetryCount) {
218  //If timer is fired, then we are in middle of
219  //vm stat collection, in such case dont delete the vm stat
220  //entry as asio may be using it
221  delete this;
222  }
223 }
static void CloseTaskFds(void)
Definition: agent_cmn.h:80
uint32_t vmi_vm_vn_uve_interval_msecs() const
Definition: agent_param.h:466
VmUveTableBase * vm_uve_table() const
Definition: agent.h:361
AgentParam * params() const
Definition: agent.h:1227
AgentUveBase * uve() const
Definition: agent.cc:913
static bool DeleteTimer(Timer *Timer)
Definition: timer.cc:222
bool Cancel()
Definition: timer.cc:150
bool running() const
Definition: timer.h:90
bool Start(int time, Handler handler, ErrorHandler error_handler=NULL)
Definition: timer.cc:108
virtual void Start()
Definition: vm_stat.cc:212
uint32_t disk_size_
Definition: vm_stat.h:65
DoneCb call_back_
Definition: vm_stat.h:63
void SendVmCpuStats()
Definition: vm_stat.cc:180
boost::asio::posix::stream_descriptor input_
Definition: vm_stat.h:58
boost::function< void(void)> DoneCb
Definition: vm_stat.h:23
virtual bool TimerExpiry()
Definition: vm_stat.cc:202
bool BuildVmMsg(UveVirtualMachineAgent *uve)
Definition: vm_stat.cc:148
uint32_t virt_memory_
Definition: vm_stat.h:47
uint32_t virtual_size_
Definition: vm_stat.h:64
void ProcessData()
Definition: vm_stat.cc:70
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 Stop()
Definition: vm_stat.cc:215
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
virtual ~VmStat()
Definition: vm_stat.cc:42
static const size_t kBufLen
Definition: vm_stat.h:20
VrouterAgentVmState::type prev_vm_state_
Definition: vm_stat.h:68
char rx_buff_[kBufLen]
Definition: vm_stat.h:56
uint32_t vm_memory_quota_
Definition: vm_stat.h:49
Timer * timer_
Definition: vm_stat.h:59
uint32_t virt_memory_peak_
Definition: vm_stat.h:48
void ReadData(const boost::system::error_code &ec, size_t read_bytes, DoneCb &cb)
Definition: vm_stat.cc:46
uint32_t mem_usage_
Definition: vm_stat.h:46
bool BuildVmStatsMsg(VirtualMachineStats *uve)
Definition: vm_stat.cc:128
std::stringstream data_
Definition: vm_stat.h:57
uint16_t prev_vm_cpu_count_
Definition: vm_stat.h:70
bool marked_delete_
Definition: vm_stat.h:60
VmStat(Agent *agent, const boost::uuids::uuid &vm_uuid)
Definition: vm_stat.cc:28
Agent * agent_
Definition: vm_stat.h:44
virtual void DispatchVmMsg(const UveVirtualMachineAgent &uve)
virtual void DispatchVmStatsMsg(const VirtualMachineStats &uve)
Definition: vm_uve_table.cc:93
void EnqueueVmStatData(VmStatData *data)
Definition: vm_uve_table.cc:79
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:139
boost::uuids::uuid uuid