COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
agent_cpu.cpp File Reference
#include "support/configCosmos.h"
#include "agent/agentclass.h"
#include "support/timeutils.h"
#include "support/jsonlib.h"
#include "support/elapsedtime.h"
#include "support/stringlib.h"
#include "support/print_utils.h"
#include "device/cpu/devicecpu.h"
#include "device/disk/devicedisk.h"
#include <stdio.h>
#include <iostream>
Include dependency graph for agent_cpu.cpp:

Functions

int32_t get_last_offset ()
 
int32_t request_bootCount (string &request, string &response, Agent *)
 
int32_t request_diskSize (string &request, string &response, Agent *)
 
int32_t request_diskUsed (string &request, string &response, Agent *)
 
int32_t request_diskFree (string &request, string &response, Agent *)
 
int32_t request_diskFreePercent (string &request, string &response, Agent *)
 
int32_t request_cpuProcess (string &request, string &response, Agent *)
 
int32_t request_load (string &request, string &response, Agent *)
 
int32_t request_mem (string &request, string &response, Agent *)
 
int32_t request_mem_kib (string &request, string &response, Agent *)
 
int32_t request_mem_percent (string &request, string &response, Agent *)
 
int32_t request_mem_total (string &request, string &response, Agent *)
 
int32_t request_mem_total_kib (string &request, string &response, Agent *)
 
int32_t request_printStatus (string &request, string &response, Agent *)
 
int main (int argc, char *argv[])
 
int32_t request_diskFreePercent (std::string &, std::string &response, Agent *)
 

Variables

bool printStatus = true
 
static string sohstring
 
static ElapsedTime et
 
static DeviceDisk deviceDisk
 
static DeviceCpu deviceCpu
 
static uint16_t cpu_cidx
 
static uint16_t cpu_didx
 
static Agentagent
 

Function Documentation

int32_t get_last_offset ( )
276 {
277  int32_t offset = 0;
278  FILE *fp = fopen(("/cosmos/nodes/" + agent->nodeName + "/last_offset").c_str(), "r");
279  if (fp != nullptr)
280  {
281  fscanf(fp, "%d", &offset);
282  fclose(fp);
283  }
284  return offset;
285 }
string nodeName
Definition: agentclass.h:367
static Agent * agent
Definition: agent_cpu.cpp:84
FILE * fp
Definition: rw_test.cpp:38
int32_t request_bootCount ( string &  request,
string &  response,
Agent  
)
376 {
377 
378  std::ifstream ifs ("/hiakasat/nodes/hiakasat/boot.count");
379  string counts;
380 
381  if (ifs.is_open()) {
382  std::getline(ifs,counts);
383  ifs.close();
384  }
385  else {
386  cout << "Error opening file";
387  }
388 
389  return ((response = counts).length());
390 }
png_uint_32 length
Definition: png.c:2173
int32_t request_diskSize ( string &  request,
string &  response,
Agent  
)
301 {
302  return ((response = std::to_string(deviceDisk.SizeGiB)).length());
303 }
string to_string(char *value)
Definition: stringlib.cpp:220
double SizeGiB
Definition: devicedisk.h:38
static DeviceDisk deviceDisk
Definition: agent_cpu.cpp:77
int32_t request_diskUsed ( string &  request,
string &  response,
Agent  
)
306 {
307  return ((response = std::to_string(deviceDisk.UsedGiB)).length());
308 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceDisk deviceDisk
Definition: agent_cpu.cpp:77
double UsedGiB
Definition: devicedisk.h:39
int32_t request_diskFree ( string &  request,
string &  response,
Agent  
)
311 {
312  // TODO: implement diskFree
313  //return (response = "%.1f", agent->cinfo->device[cpu_cidx].cpugib));
314 
315  // in the mean time use this
316  return ((response = std::to_string(deviceDisk.FreeGiB)).length());
317 
318 }
string to_string(char *value)
Definition: stringlib.cpp:220
double FreeGiB
Definition: devicedisk.h:40
static DeviceDisk deviceDisk
Definition: agent_cpu.cpp:77
int32_t request_diskFreePercent ( string &  request,
string &  response,
Agent  
)
int32_t request_cpuProcess ( string &  request,
string &  response,
Agent  
)
334  {
335 
336  return ((response = std::to_string(deviceCpu.percentUseForCurrentProcess)).length());
337 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceCpu deviceCpu
Definition: agent_cpu.cpp:78
double percentUseForCurrentProcess
Definition: devicecpu.h:244
int32_t request_load ( string &  request,
string &  response,
Agent  
)
330 {
331  return ((response = std::to_string(deviceCpu.load)).length());
332 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceCpu deviceCpu
Definition: agent_cpu.cpp:78
double load
Definition: devicecpu.h:255
int32_t request_mem ( string &  request,
string &  response,
Agent  
)
342 {
343  return ((response = std::to_string(deviceCpu.virtualMemoryUsed)).length());
344 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceCpu deviceCpu
Definition: agent_cpu.cpp:78
double virtualMemoryUsed
Definition: devicecpu.h:252
int32_t request_mem_kib ( string &  request,
string &  response,
Agent  
)
348 {
349  return ((response = std::to_string(deviceCpu.virtualMemoryUsed/1024.)).length());
350 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceCpu deviceCpu
Definition: agent_cpu.cpp:78
double virtualMemoryUsed
Definition: devicecpu.h:252
png_uint_32 length
Definition: png.c:2173
int32_t request_mem_percent ( string &  request,
string &  response,
Agent  
)
354 {
355 
356  return ((response = std::to_string(deviceCpu.getVirtualMemoryUsedPercent())).length());
357 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceCpu deviceCpu
Definition: agent_cpu.cpp:78
double getVirtualMemoryUsedPercent()
Definition: devicecpu.cpp:183
int32_t request_mem_total ( string &  request,
string &  response,
Agent  
)
361 {
362  return ((response = std::to_string(deviceCpu.getVirtualMemoryTotal())).length());
363 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceCpu deviceCpu
Definition: agent_cpu.cpp:78
double getVirtualMemoryTotal()
Definition: devicecpu.cpp:168
int32_t request_mem_total_kib ( string &  request,
string &  response,
Agent  
)
367 {
368  return ((response = std::to_string(deviceCpu.getVirtualMemoryTotal()/1024)).length());
369 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceCpu deviceCpu
Definition: agent_cpu.cpp:78
double getVirtualMemoryTotal()
Definition: devicecpu.cpp:168
int32_t request_printStatus ( string &  request,
string &  response,
Agent  
)
395 {
396 
397  sscanf(request.c_str(),"%*s %d",&printStatus);
398  cout << "printStatus is " << printStatus << endl;
399 
400  return 0;
401 }
bool printStatus
Definition: agent_cpu.cpp:45
int main ( int  argc,
char *  argv[] 
)
87 {
88  int32_t iretn;
89 
90  cout<<"size of devicestruc = "<<sizeof(devicestruc)<<endl;
91  if (argc == 2) {
92  agent = new Agent(argv[1], "cpu", 15.);
93  } else {
94  agent = new Agent("", "cpu", 15.);
95  }
96 
97  if ((iretn = agent->wait()) < 0) {
98  fprintf(agent->get_debug_fd(), "%16.10f %s Failed to start Agent %s on Node %s Dated %s : %s\n",currentmjd(), mjd2iso8601(currentmjd()).c_str(), agent->getAgent().c_str(), agent->getNode().c_str(), utc2iso8601(data_ctime(argv[0])).c_str(), cosmos_error_string(iretn).c_str());
99  exit(iretn);
100  } else {
101  fprintf(agent->get_debug_fd(), "%16.10f %s Started Agent %s on Node %s Dated %s\n",currentmjd(), mjd2iso8601(currentmjd()).c_str(), agent->getAgent().c_str(), agent->getNode().c_str(), utc2iso8601(data_ctime(argv[0])).c_str());
102  }
103 
105 
106  if (iretn < 0)
107  {
108  fprintf(agent->get_debug_fd(), "Failed to add CPU %s\n", cosmos_error_string(iretn).c_str());
109  agent->shutdown();
110  exit(1);
111  }
112  cpu_cidx = agent->cinfo->pieces[static_cast <uint16_t>(iretn)].cidx;
113  cpu_didx = agent->cinfo->device[cpu_cidx].didx;
114 
115  sohstring = "{\"node_downtime\"";
116  sohstring += ",\"device_cpu_utc_00" + std::to_string(cpu_didx) + "\"";
117  sohstring += ",\"device_cpu_temp_00" + std::to_string(cpu_didx) + "\"";
118  sohstring += ",\"device_cpu_maxgib_00" + std::to_string(cpu_didx) + "\"";
119  sohstring += ",\"device_cpu_gib_00" + std::to_string(cpu_didx) + "\"";
120  sohstring += ",\"device_cpu_maxload_00" + std::to_string(cpu_didx) + "\"";
121  sohstring += ",\"device_cpu_load_00" + std::to_string(cpu_didx) + "\"";
122  sohstring += ",\"device_cpu_uptime_00" + std::to_string(cpu_didx) + "\"";
123  sohstring += ",\"device_cpu_boot_count_00" + std::to_string(cpu_didx) + "\"";
124 
125  static const double GiB = 1024. * 1024. * 1024.;
126 
127  agent->cinfo->device[cpu_cidx].cpu.load = static_cast <float>(deviceCpu.getLoad());
128  agent->cinfo->device[cpu_cidx].cpu.gib = static_cast <float>(deviceCpu.getVirtualMemoryUsed()/GiB);
129  agent->cinfo->device[cpu_cidx].cpu.maxgib = static_cast <float>(deviceCpu.getVirtualMemoryTotal()/GiB);
130  agent->cinfo->device[cpu_cidx].cpu.maxload = deviceCpu.getCpuCount();
133 
134  vector <DeviceDisk::info> dinfo = deviceDisk.getInfo();
135  for (uint16_t i=0; i<dinfo.size(); ++i)
136  {
137  char name[10];
138  sprintf(name, "disk_%02u", i);
139  iretn = json_createpiece(agent->cinfo, name, DeviceType::DISK);
140  if (iretn < 0)
141  {
142  fprintf(agent->get_debug_fd(), "Failed to add DISK %s : %s\n", dinfo[i].mount.c_str(), cosmos_error_string(iretn).c_str());
143  agent->shutdown();
144  exit(1);
145  }
146  uint16_t cidx = agent->cinfo->pieces[static_cast <uint16_t>(iretn)].cidx;
147 // uint16_t didx = agent->cinfo->device[cidx].didx;
148  strncpy(agent->cinfo->device[cidx].disk.path, dinfo[i].mount.c_str(), COSMOS_MAX_NAME);
149  sohstring += ",\"device_disk_utc_00" + std::to_string(cpu_didx) + "\"";
150  sohstring += ",\"device_disk_temp_00" + std::to_string(cpu_didx) + "\"";
151  sohstring += ",\"device_disk_path_00" + std::to_string(cpu_didx) + "\"";
152  sohstring += ",\"device_disk_gib_00" + std::to_string(cpu_didx) + "\"";
153  sohstring += ",\"device_disk_maxgib_00" + std::to_string(cpu_didx) + "\"";
154  sohstring += ",\"device_disk_utilization_00" + std::to_string(cpu_didx) + "\"";
155  sohstring += ",\"device_disk_temp_00" + std::to_string(cpu_didx) + "\"";
156  }
157  sohstring += "}";
159  printf("SOH String: %s\n", sohstring.c_str());
160 
161 // json_dump_node(agent->cinfo);
162 
163  // TODO: determine number of disks automatically
164  PrintUtils print;
165  print.scalar("Number of Disks: ",agent->cinfo->devspec.disk_cnt);
166  print.endline();
167  print.scalar("Number of Cores: ",agent->cinfo->device[cpu_cidx].cpu.maxload);
168  print.endline();
169 
170  fprintf(agent->get_debug_fd(), "CPU Agent initialized\n");
171 
172 // agent->add_request("soh",request_soh,"","current state of health message");
173  agent->add_request("diskSize",request_diskSize,"","disk size in GB");
174  agent->add_request("diskUsed",request_diskUsed,"","disk used in GB");
175  agent->add_request("diskFree",request_diskFree,"","disk free in GB");
176  agent->add_request("diskFreePercent",request_diskFreePercent,"","disk free in %");
177  agent->add_request("mem",request_mem,"","current virtual memory used in Bytes");
178  agent->add_request("mem_kib",request_mem_kib,"","current virtual memory used in KiB");
179  agent->add_request("mem_percent",request_mem_percent,"","memory percentage");
180  agent->add_request("mem_total",request_mem_total,"","total memory in Bytes");
181  agent->add_request("mem_total_kib",request_mem_total_kib,"","total memory in KiB");
182  agent->add_request("load",request_load,"","current CPU load (0-1 is good, >1 is overloaded)");
183  agent->add_request("cpuProc",request_cpuProcess,"","the %CPU usage for this process");
184  agent->add_request("printStatus",request_printStatus,"","print the status data");
185  agent->add_request("bootCount",request_bootCount,"","reboot count");
186 
187  et.start();
188 
189  agent->cinfo->agent[0].aprd = agent->cinfo->agent[0].beat.bprd;
191 
192  // Initialize temperature Fit
193  LsFit cputemp(4, 1);
194  // Start performing the body of the agent
195  agent->debug_level = 0;
196  while(agent->running())
197  {
198  agent->cinfo->device[cpu_cidx].utc = currentmjd();
199  if (agent->debug_level)
200  {
201  fprintf(agent->get_debug_fd(), "%16.10f ", agent->cinfo->device[cpu_cidx].utc);
202  }
204 
205  // get cpu info
206  if (agent->cinfo->devspec.cpu_cnt)
207  {
208  agent->cinfo->device[cpu_cidx].cpu.uptime = deviceCpu.getUptime();
209  agent->cinfo->device[cpu_cidx].cpu.boot_count = deviceCpu.getBootCount();
210  agent->cinfo->device[cpu_cidx].cpu.load = deviceCpu.getLoad();
214  }
215  if (agent->debug_level)
216  {
217  fprintf(agent->get_debug_fd(), "Load %6.2f %6.2f ", agent->cinfo->device[cpu_cidx].cpu.load, agent->cinfo->device[cpu_cidx].cpu.maxload);
218  fprintf(agent->get_debug_fd(), "Memory %6.2f %6.2f ", agent->cinfo->device[cpu_cidx].cpu.gib, agent->cinfo->device[cpu_cidx].cpu.maxgib);
219  }
220 
221  // get disk info
222  for (size_t i=0; i<agent->cinfo->devspec.disk_cnt; ++i)
223  {
225 
226  string node_path = agent->cinfo->device[agent->cinfo->devspec.disk[i]].disk.path;
227 
228  agent->cinfo->device[agent->cinfo->devspec.disk[i]].disk.gib = deviceDisk.getUsedGiB(node_path);
229  agent->cinfo->device[agent->cinfo->devspec.disk[i]].disk.maxgib = deviceDisk.getSizeGiB(node_path);
230  if (agent->debug_level)
231  {
232  fprintf(agent->get_debug_fd(), "%s %6.2f %6.2f ", node_path.c_str(), agent->cinfo->device[agent->cinfo->devspec.disk[i]].disk.gib, agent->cinfo->device[agent->cinfo->devspec.disk[i]].disk.maxgib);
233  }
234  }
235 
236  // get temperature
237  string response;
238  iretn = data_execute("get_cpu_temperature", response);
239  if (iretn > 0)
240  {
241  float ctemp = stof(response);
242  cputemp.update(currentmjd(), ctemp);
243  agent->cinfo->device[cpu_cidx].temp = cputemp.eval(currentmjd());
244  }
245 
246 // FILE *cmd_pipe = popen("/cosmos/scripts/get_cpu_temperature", "r");
247 
248 // if (cmd_pipe != nullptr)
249 // {
250 // float ctemp;
251 // fscanf(cmd_pipe, "%f", &ctemp);
252 // pclose( cmd_pipe );
253 
254 // cputemp.update(currentmjd(), ctemp);
255 // agent->cinfo->device[cpu_cidx].temp = cputemp.eval(currentmjd());
256 // }
257 
258  if (agent->debug_level)
259  {
260  fprintf(agent->get_debug_fd(), "\n");
261  }
262 
264  }
265 
266  agent->shutdown();
267 
268  // Start our own thread
269 // agent_cpu();
270 
271  return 0;
272 
273 }
int32_t request_mem_kib(string &request, string &response, Agent *)
Definition: agent_cpu.cpp:347
uint16_t disk_cnt
Definition: jsondef.h:3868
uint16_t debug_level
Flag for level of debugging, keep it public so that it can be controlled from the outside...
Definition: agentclass.h:362
int32_t finish_active_loop()
Definition: agentclass.cpp:355
FILE * get_debug_fd(double mjd=0.)
Definition: agentclass.cpp:2645
uint16_t getCpuCount()
Definition: devicecpu.cpp:56
Device structure.
Definition: jsondef.h:3692
int32_t request_diskSize(string &request, string &response, Agent *)
Definition: agent_cpu.cpp:300
Definition: print_utils.h:60
string nodeName
Definition: agentclass.h:367
int i
Definition: rw_test.cpp:37
string getNode()
Listen for heartbeat.
Definition: agentclass.cpp:2607
#define COSMOS_MAX_NAME
Largest JSON name.
Definition: cosmos-defs.h:55
static const double GiB
Definition: devicedisk.h:30
static uint16_t cpu_didx
Definition: agent_cpu.cpp:82
uint16_t getBootCount()
Definition: devicecpu.cpp:65
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceCpu deviceCpu
Definition: agent_cpu.cpp:78
int iretn
Definition: rw_test.cpp:37
int32_t wait(State state=State::RUN, double waitsec=10.)
Definition: agentclass.cpp:398
static Agent * agent
Definition: agent_cpu.cpp:84
int32_t request_mem_total(string &request, string &response, Agent *)
Definition: agent_cpu.cpp:360
int32_t set_sohstring(string list)
Set Limited SOH string.
Definition: agentclass.cpp:641
int numProcessors
Definition: devicecpu.h:245
int32_t request_mem_total_kib(string &request, string &response, Agent *)
Definition: agent_cpu.cpp:366
double getVirtualMemoryTotal()
Definition: devicecpu.cpp:168
Definition: mathlib.h:361
vector< devicestruc > device
Vector of all general (common) information for devices (components) in node.
Definition: jsondef.h:4238
nodestruc node
Structure for summary information in node.
Definition: jsondef.h:4220
int32_t request_diskFreePercent(string &request, string &response, Agent *)
double getPercentUseForCurrentProcess()
Definition: devicecpu.cpp:191
string cosmos_error_string(int32_t cosmos_errno)
Definition: cosmos-errno.cpp:45
int32_t data_execute(string cmd, string &result, string shell)
Definition: datalib.cpp:1947
uint16_t running()
Check if we&#39;re supposed to be running.
Definition: agentclass.cpp:391
int32_t request_diskUsed(string &request, string &response, Agent *)
Definition: agent_cpu.cpp:305
void scalar(string prefix, double s, string suffix)
Definition: print_utils.cpp:279
void start()
ElapsedTime::start.
Definition: elapsedtime.cpp:203
int32_t request_load(string &request, string &response, Agent *)
Definition: agent_cpu.cpp:329
int32_t request_mem(string &request, string &response, Agent *)
Definition: agent_cpu.cpp:341
int32_t request_diskFree(string &request, string &response, Agent *)
Definition: agent_cpu.cpp:310
uint32_t downtime
Seconds Node will be down.
Definition: jsondef.h:3582
string getAgent()
Definition: agentclass.cpp:2609
int32_t add_request(string token, external_request_function function, string synopsis="", string description="")
Add internal request to Agent request list with description and synopsis.
Definition: agentclass.cpp:312
vector< uint16_t > disk
Definition: jsondef.h:3901
Definition: agentclass.h:139
static string sohstring
Definition: agent_cpu.cpp:73
int32_t get_last_offset()
Definition: agent_cpu.cpp:275
string name
Definition: cubesat2obj.cpp:6
double getLoad()
Definition: devicecpu.cpp:38
int32_t shutdown()
Shutdown agent gracefully.
Definition: agentclass.cpp:366
uint16_t cpu_cnt
Definition: jsondef.h:3867
double data_ctime(string path)
Definition: datalib.cpp:1910
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
int32_t request_printStatus(string &request, string &response, Agent *)
Definition: agent_cpu.cpp:394
int32_t request_mem_percent(string &request, string &response, Agent *)
Definition: agent_cpu.cpp:353
string utc2iso8601(double utc)
ISO 8601 version of time.
Definition: timelib.cpp:1286
vector< agentstruc > agent
Single entry vector for agent information.
Definition: jsondef.h:4247
Processing Unit.
Definition: jsondef.h:502
double getUsedGiB(string path)
Definition: devicedisk.cpp:134
vector< piecestruc > pieces
Vector of all pieces in node.
Definition: jsondef.h:4232
static DeviceDisk deviceDisk
Definition: agent_cpu.cpp:77
int32_t start_active_loop()
Definition: agentclass.cpp:347
int32_t json_createpiece(cosmosstruc *cinfo, string name, DeviceType ctype, double emi, double abs, double hcap, double hcon, double density)
Create new piece.
Definition: jsonlib.cpp:722
cosmosstruc * cinfo
Definition: agentclass.h:346
uint32_t getUptime()
Definition: devicecpu.cpp:74
vector< info > getInfo()
Definition: devicedisk.cpp:179
double getSizeGiB(string path)
Definition: devicedisk.cpp:104
devspecstruc devspec
Structure for devices (components) special data in node, by type.
Definition: jsondef.h:4241
Disk Drive.
Definition: jsondef.h:548
string mjd2iso8601(double mjd)
Definition: timelib.cpp:1316
int32_t request_cpuProcess(string &request, string &response, Agent *)
Definition: agent_cpu.cpp:334
static uint16_t cpu_cidx
Definition: agent_cpu.cpp:81
double getVirtualMemoryUsed()
Definition: devicecpu.cpp:110
int32_t request_bootCount(string &request, string &response, Agent *)
Definition: agent_cpu.cpp:375
static ElapsedTime et
Definition: agent_cpu.cpp:76
int32_t request_diskFreePercent ( std::string &  ,
std::string &  response,
Agent  
)
321 {
322  return ((response = std::to_string(deviceDisk.FreePercent)).length());
323 }
string to_string(char *value)
Definition: stringlib.cpp:220
double FreePercent
Definition: devicedisk.h:41
static DeviceDisk deviceDisk
Definition: agent_cpu.cpp:77

Variable Documentation

bool printStatus = true
string sohstring
static
ElapsedTime et
static
DeviceDisk deviceDisk
static
DeviceCpu deviceCpu
static
uint16_t cpu_cidx
static
uint16_t cpu_didx
static
Agent* agent
static