COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
agent_cpu-2-0.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-2-0.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 ( )
279 {
280  int32_t offset = 0;
281  FILE *fp = fopen(("/cosmos/nodes/" + agent->nodeName + "/last_offset").c_str(), "r");
282  if (fp != nullptr)
283  {
284  fscanf(fp, "%d", &offset);
285  fclose(fp);
286  }
287  return offset;
288 }
string nodeName
Definition: agentclass.h:367
static Agent * agent
Definition: agent_cpu-2-0.cpp:83
FILE * fp
Definition: rw_test.cpp:38
int32_t request_bootCount ( string &  request,
string &  response,
Agent  
)
368 {
369 
370  std::ifstream ifs ("/hiakasat/nodes/hiakasat/boot.count");
371  string counts;
372 
373  if (ifs.is_open()) {
374  std::getline(ifs,counts);
375  ifs.close();
376  }
377  else {
378  cout << "Error opening file";
379  }
380 
381  return ((response = counts).length());
382 }
png_uint_32 length
Definition: png.c:2173
int32_t request_diskSize ( string &  request,
string &  response,
Agent  
)
294 {
295  return ((response = std::to_string(deviceDisk.SizeGiB)).length());
296 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceDisk deviceDisk
Definition: agent_cpu-2-0.cpp:76
double SizeGiB
Definition: devicedisk.h:38
int32_t request_diskUsed ( string &  request,
string &  response,
Agent  
)
299 {
300  return ((response = std::to_string(deviceDisk.UsedGiB)).length());
301 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceDisk deviceDisk
Definition: agent_cpu-2-0.cpp:76
double UsedGiB
Definition: devicedisk.h:39
int32_t request_diskFree ( string &  request,
string &  response,
Agent  
)
304 {
305  // TODO: implement diskFree
306  //return (response = "%.1f", agent->cinfo->device[cpu_cidx].cpugib));
307 
308  // in the mean time use this
309  return ((response = std::to_string(deviceDisk.FreeGiB)).length());
310 
311 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceDisk deviceDisk
Definition: agent_cpu-2-0.cpp:76
double FreeGiB
Definition: devicedisk.h:40
int32_t request_diskFreePercent ( string &  request,
string &  response,
Agent  
)
int32_t request_cpuProcess ( string &  request,
string &  response,
Agent  
)
327  {
328 
329  return ((response = std::to_string(deviceCpu.percentUseForCurrentProcess)).length());
330 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceCpu deviceCpu
Definition: agent_cpu-2-0.cpp:77
double percentUseForCurrentProcess
Definition: devicecpu.h:244
int32_t request_load ( string &  request,
string &  response,
Agent  
)
323 {
324  return ((response = std::to_string(deviceCpu.load)).length());
325 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceCpu deviceCpu
Definition: agent_cpu-2-0.cpp:77
double load
Definition: devicecpu.h:255
int32_t request_mem ( string &  request,
string &  response,
Agent  
)
335 {
336  return ((response = std::to_string(deviceCpu.virtualMemoryUsed)).length());
337 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceCpu deviceCpu
Definition: agent_cpu-2-0.cpp:77
double virtualMemoryUsed
Definition: devicecpu.h:252
int32_t request_mem_kib ( string &  request,
string &  response,
Agent  
)
341 {
342  return ((response = std::to_string(deviceCpu.virtualMemoryUsed/1024.)).length());
343 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceCpu deviceCpu
Definition: agent_cpu-2-0.cpp:77
double virtualMemoryUsed
Definition: devicecpu.h:252
png_uint_32 length
Definition: png.c:2173
int32_t request_mem_percent ( string &  request,
string &  response,
Agent  
)
347 {
348  return ((response = std::to_string(deviceCpu.getVirtualMemoryUsedPercent())).length());
349 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceCpu deviceCpu
Definition: agent_cpu-2-0.cpp:77
double getVirtualMemoryUsedPercent()
Definition: devicecpu.cpp:183
int32_t request_mem_total ( string &  request,
string &  response,
Agent  
)
353 {
354  return ((response = std::to_string(deviceCpu.getVirtualMemoryTotal())).length());
355 }
string to_string(char *value)
Definition: stringlib.cpp:220
double getVirtualMemoryTotal()
Definition: devicecpu.cpp:168
static DeviceCpu deviceCpu
Definition: agent_cpu-2-0.cpp:77
int32_t request_mem_total_kib ( string &  request,
string &  response,
Agent  
)
359 {
360  return ((response = std::to_string(deviceCpu.getVirtualMemoryTotal()/1024)).length());
361 }
string to_string(char *value)
Definition: stringlib.cpp:220
double getVirtualMemoryTotal()
Definition: devicecpu.cpp:168
static DeviceCpu deviceCpu
Definition: agent_cpu-2-0.cpp:77
int32_t request_printStatus ( string &  request,
string &  response,
Agent  
)
387 {
388 
389  sscanf(request.c_str(),"%*s %d",&printStatus);
390  cout << "printStatus is " << printStatus << endl;
391 
392  return 0;
393 }
bool printStatus
Definition: agent_cpu-2-0.cpp:45
int main ( int  argc,
char *  argv[] 
)
86 {
87  int32_t iretn;
88 
89  cout<<"size of devicestruc = "<<sizeof(devicestruc)<<endl;
90  if (argc == 2) {
91  agent = new Agent(argv[1], "cpu", 15.);
92  } else {
93  agent = new Agent("", "cpu", 15.);
94  }
95 
96  if ((iretn = agent->wait()) < 0) {
97  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());
98  exit(iretn);
99  } else {
100  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());
101  }
102 
103  // NS1
104  // iretn = json_createpiece(agent->cinfo, agent->nodeName + "_cpu", DeviceType::CPU);
105  // NS2 ... hmm... nothing really happens inside json_createpiece except direct memory calls and adding Namespace 1.0 names (which already exist as default for Namespace 2.0, so no need to change
107 
108  if (iretn < 0)
109  {
110  fprintf(agent->get_debug_fd(), "Failed to add CPU %s\n", cosmos_error_string(iretn).c_str());
111  agent->shutdown();
112  exit(1);
113  }
114  cpu_cidx = agent->cinfo->pieces[static_cast <uint16_t>(iretn)].cidx;
115  cpu_didx = agent->cinfo->device[cpu_cidx].didx;
116 
117  sohstring = "{\"node_downtime\"";
118  sohstring += ",\"device_cpu_utc_00" + std::to_string(cpu_didx) + "\"";
119  sohstring += ",\"device_cpu_temp_00" + std::to_string(cpu_didx) + "\"";
120  sohstring += ",\"device_cpu_maxgib_00" + std::to_string(cpu_didx) + "\"";
121  sohstring += ",\"device_cpu_gib_00" + std::to_string(cpu_didx) + "\"";
122  sohstring += ",\"device_cpu_maxload_00" + std::to_string(cpu_didx) + "\"";
123  sohstring += ",\"device_cpu_load_00" + std::to_string(cpu_didx) + "\"";
124  sohstring += ",\"device_cpu_uptime_00" + std::to_string(cpu_didx) + "\"";
125  sohstring += ",\"device_cpu_boot_count_00" + std::to_string(cpu_didx) + "\"";
126 
127  static const double GiB = 1024. * 1024. * 1024.;
128 
129  // update CPU info
130  agent->cinfo->device[cpu_cidx].cpu.load = static_cast <float>(deviceCpu.getLoad());
131  agent->cinfo->device[cpu_cidx].cpu.gib = static_cast <float>(deviceCpu.getVirtualMemoryUsed()/GiB);
132  agent->cinfo->device[cpu_cidx].cpu.maxgib = static_cast <float>(deviceCpu.getVirtualMemoryTotal()/GiB);
133  agent->cinfo->device[cpu_cidx].cpu.maxload = deviceCpu.getCpuCount();
136 
137  vector <DeviceDisk::info> dinfo = deviceDisk.getInfo();
138  for (uint16_t i=0; i<dinfo.size(); ++i)
139  {
140  char name[10];
141  sprintf(name, "disk_%02u", i);
142  // NS1
143  //iretn = json_createpiece(agent->cinfo, name, DeviceType::DISK);
144  // NS2 ... hmm... nothing really happens inside json_createpiece except direct memory calls and adding Namespace 1.0 names (which already exist as default for Namespace 2.0, so no need to change
145  iretn = json_createpiece(agent->cinfo, name, DeviceType::DISK);
146  if (iretn < 0)
147  {
148  fprintf(agent->get_debug_fd(), "Failed to add DISK %s : %s\n", dinfo[i].mount.c_str(), cosmos_error_string(iretn).c_str());
149  agent->shutdown();
150  exit(1);
151  }
152  uint16_t cidx = agent->cinfo->pieces[static_cast <uint16_t>(iretn)].cidx;
153  strncpy(agent->cinfo->device[cidx].disk.path, dinfo[i].mount.c_str(), COSMOS_MAX_NAME);
154  sohstring += ",\"device_disk_utc_00" + std::to_string(cpu_didx) + "\"";
155  sohstring += ",\"device_disk_temp_00" + std::to_string(cpu_didx) + "\"";
156  sohstring += ",\"device_disk_path_00" + std::to_string(cpu_didx) + "\"";
157  sohstring += ",\"device_disk_gib_00" + std::to_string(cpu_didx) + "\"";
158  sohstring += ",\"device_disk_maxgib_00" + std::to_string(cpu_didx) + "\"";
159  sohstring += ",\"device_disk_utilization_00" + std::to_string(cpu_didx) + "\"";
160  sohstring += ",\"device_disk_temp_00" + std::to_string(cpu_didx) + "\"";
161  }
162  sohstring += "}";
164  printf("SOH String: %s\n", sohstring.c_str());
165 
166  // TODO: determine number of disks automatically
167  PrintUtils print;
168  print.scalar("Number of Disks: ",agent->cinfo->devspec.disk_cnt);
169  print.endline();
170  print.scalar("Number of Cores: ",agent->cinfo->device[cpu_cidx].cpu.maxload);
171  print.endline();
172 
173  fprintf(agent->get_debug_fd(), "CPU Agent initialized\n");
174 
175 // agent->add_request("soh",request_soh,"","current state of health message");
176  agent->add_request("diskSize",request_diskSize,"","disk size in GB");
177  agent->add_request("diskUsed",request_diskUsed,"","disk used in GB");
178  agent->add_request("diskFree",request_diskFree,"","disk free in GB");
179  agent->add_request("diskFreePercent",request_diskFreePercent,"","disk free in %");
180  agent->add_request("mem",request_mem,"","current virtual memory used in Bytes");
181  agent->add_request("mem_kib",request_mem_kib,"","current virtual memory used in KiB");
182  agent->add_request("mem_percent",request_mem_percent,"","memory percentage");
183  agent->add_request("mem_total",request_mem_total,"","total memory in Bytes");
184  agent->add_request("mem_total_kib",request_mem_total_kib,"","total memory in KiB");
185  agent->add_request("load",request_load,"","current CPU load (0-1 is good, >1 is overloaded)");
186  agent->add_request("cpuProc",request_cpuProcess,"","the %CPU usage for this process");
187  agent->add_request("printStatus",request_printStatus,"","print the status data");
188  agent->add_request("bootCount",request_bootCount,"","reboot count");
189 
190  et.start();
191 
192  agent->cinfo->agent[0].aprd = agent->cinfo->agent[0].beat.bprd;
194 
195  // Initialize temperature Fit
196  LsFit cputemp(4, 1);
197  // Start performing the body of the agent
198  agent->debug_level = 0;
199  while(agent->running())
200  {
201  agent->cinfo->device[cpu_cidx].utc = currentmjd();
202  if (agent->debug_level)
203  {
204  fprintf(agent->get_debug_fd(), "%16.10f ", agent->cinfo->device[cpu_cidx].utc);
205  }
207 
208  // get cpu info
209  if (agent->cinfo->devspec.cpu_cnt)
210  {
211  agent->cinfo->device[cpu_cidx].cpu.uptime = deviceCpu.getUptime();
212  agent->cinfo->device[cpu_cidx].cpu.boot_count = deviceCpu.getBootCount();
213  agent->cinfo->device[cpu_cidx].cpu.load = deviceCpu.getLoad();
217  }
218  if (agent->debug_level)
219  {
220  fprintf(agent->get_debug_fd(), "Load %6.2f %6.2f ", agent->cinfo->device[cpu_cidx].cpu.load, agent->cinfo->device[cpu_cidx].cpu.maxload);
221  fprintf(agent->get_debug_fd(), "Memory %6.2f %6.2f ", agent->cinfo->device[cpu_cidx].cpu.gib, agent->cinfo->device[cpu_cidx].cpu.maxgib);
222  }
223 
224  // get disk info
225  for (size_t i=0; i<agent->cinfo->devspec.disk_cnt; ++i)
226  {
228 
229  string node_path = agent->cinfo->device[agent->cinfo->devspec.disk[i]].disk.path;
230 
231  agent->cinfo->device[agent->cinfo->devspec.disk[i]].disk.gib = deviceDisk.getUsedGiB(node_path);
232  agent->cinfo->device[agent->cinfo->devspec.disk[i]].disk.maxgib = deviceDisk.getSizeGiB(node_path);
233  if (agent->debug_level)
234  {
235  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);
236  }
237  }
238 
239  // get temperature
240  string response;
241  iretn = data_execute("get_cpu_temperature", response);
242  if (iretn > 0)
243  {
244  float ctemp = stof(response);
245  cputemp.update(currentmjd(), ctemp);
246  agent->cinfo->device[cpu_cidx].temp = cputemp.eval(currentmjd());
247  }
248 
249 // FILE *cmd_pipe = popen("/cosmos/scripts/get_cpu_temperature", "r");
250 
251 // if (cmd_pipe != nullptr)
252 // {
253 // float ctemp;
254 // fscanf(cmd_pipe, "%f", &ctemp);
255 // pclose( cmd_pipe );
256 
257 // cputemp.update(currentmjd(), ctemp);
258 // agent->cinfo->device[cpu_cidx].temp = cputemp.eval(currentmjd());
259 // }
260 
261  if (agent->debug_level)
262  {
263  fprintf(agent->get_debug_fd(), "\n");
264  }
265 
267  }
268 
269  agent->shutdown();
270 
271  // Start our own thread
272 // agent_cpu();
273 
274  return 0;
275 
276 }
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 request_diskFreePercent(string &request, string &response, Agent *)
int32_t finish_active_loop()
Definition: agentclass.cpp:355
int32_t request_mem_total_kib(string &request, string &response, Agent *)
Definition: agent_cpu-2-0.cpp:358
static uint16_t cpu_cidx
Definition: agent_cpu-2-0.cpp:80
FILE * get_debug_fd(double mjd=0.)
Definition: agentclass.cpp:2645
uint16_t getCpuCount()
Definition: devicecpu.cpp:56
Device structure.
Definition: jsondef.h:3692
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
int32_t request_load(string &request, string &response, Agent *)
Definition: agent_cpu-2-0.cpp:322
static ElapsedTime et
Definition: agent_cpu-2-0.cpp:75
#define COSMOS_MAX_NAME
Largest JSON name.
Definition: cosmos-defs.h:55
static const double GiB
Definition: devicedisk.h:30
uint16_t getBootCount()
Definition: devicecpu.cpp:65
int32_t request_mem(string &request, string &response, Agent *)
Definition: agent_cpu-2-0.cpp:334
int32_t request_printStatus(string &request, string &response, Agent *)
Definition: agent_cpu-2-0.cpp:386
string to_string(char *value)
Definition: stringlib.cpp:220
int iretn
Definition: rw_test.cpp:37
int32_t request_mem_percent(string &request, string &response, Agent *)
Definition: agent_cpu-2-0.cpp:346
int32_t wait(State state=State::RUN, double waitsec=10.)
Definition: agentclass.cpp:398
int32_t request_diskSize(string &request, string &response, Agent *)
Definition: agent_cpu-2-0.cpp:293
int32_t set_sohstring(string list)
Set Limited SOH string.
Definition: agentclass.cpp:641
int numProcessors
Definition: devicecpu.h:245
int32_t request_mem_kib(string &request, string &response, Agent *)
Definition: agent_cpu-2-0.cpp:340
int32_t request_diskUsed(string &request, string &response, Agent *)
Definition: agent_cpu-2-0.cpp:298
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
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
static string sohstring
Definition: agent_cpu-2-0.cpp:72
static DeviceCpu deviceCpu
Definition: agent_cpu-2-0.cpp:77
void scalar(string prefix, double s, string suffix)
Definition: print_utils.cpp:279
void start()
ElapsedTime::start.
Definition: elapsedtime.cpp:203
static DeviceDisk deviceDisk
Definition: agent_cpu-2-0.cpp:76
uint32_t downtime
Seconds Node will be down.
Definition: jsondef.h:3582
string getAgent()
Definition: agentclass.cpp:2609
int32_t request_diskFree(string &request, string &response, Agent *)
Definition: agent_cpu-2-0.cpp:303
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
int32_t request_mem_total(string &request, string &response, Agent *)
Definition: agent_cpu-2-0.cpp:352
Definition: agentclass.h:139
string name
Definition: cubesat2obj.cpp:6
int32_t request_bootCount(string &request, string &response, Agent *)
Definition: agent_cpu-2-0.cpp:367
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
static Agent * agent
Definition: agent_cpu-2-0.cpp:83
int32_t get_last_offset()
Definition: agent_cpu-2-0.cpp:278
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
static uint16_t cpu_didx
Definition: agent_cpu-2-0.cpp:81
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
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
int32_t request_cpuProcess(string &request, string &response, Agent *)
Definition: agent_cpu-2-0.cpp:327
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
double getVirtualMemoryUsed()
Definition: devicecpu.cpp:110
int32_t request_diskFreePercent ( std::string &  ,
std::string &  response,
Agent  
)
314 {
315  return ((response = std::to_string(deviceDisk.FreePercent)).length());
316 }
string to_string(char *value)
Definition: stringlib.cpp:220
static DeviceDisk deviceDisk
Definition: agent_cpu-2-0.cpp:76
double FreePercent
Definition: devicedisk.h:41

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