fbzmq/service/monitor/SystemMetrics.cpp
folly::Optional<double>
SystemMetrics::getCPUpercentage() {
  struct rusage usage;
  getrusage(RUSAGE_SELF, &usage);

  ProcCpuTime nowCpuTime(usage);
  folly::Optional<double> cpuPct{folly::none};

  // calculate the CPU% = (process time diff) / (time elapsed) * 100
  if (prevCpuTime.timestamp != 0 && // has cached before
      nowCpuTime.timestamp > prevCpuTime.timestamp &&
      nowCpuTime.totalTime > prevCpuTime.totalTime) {
    uint64_t timestampDiff = nowCpuTime.timestamp - prevCpuTime.timestamp;
    uint64_t procTimeDiff = nowCpuTime.totalTime - prevCpuTime.totalTime;
    cpuPct = ((double)procTimeDiff / (double)timestampDiff) * 100;
  }

  // update the cache for next CPU% update
  prevCpuTime = nowCpuTime;

  return cpuPct;
}
/* Return CPU% the process used
 / This need to be called twice to get the time difference
 / and calculate the CPU%.
 /
 / It will return folly::none when:
 /    1. first time query
 /    2. get invalid time:
 /        - previous timestamp > current timestamp
 /        - preivous total used time > current total used time
*/
Return CPU% the process used
This need to be called twice to get the time difference
and calculate the CPU%.

It will return folly::none when:
1. first time query
2. get invalid time:
- previous timestamp > current timestamp
- preivous total used time > current total used time
Return CPU% the process used This need to be called twice to get the time difference and calculate the CPU% . It will return folly :: none when : 1 . first time query 2 . get invalid time : - previous timestamp > current timestamp - preivous total used time > current total used time
folly::Optional<double>
SystemMetrics::getCPUpercentage() {
  struct rusage usage;
  getrusage(RUSAGE_SELF, &usage);
  ProcCpuTime nowCpuTime(usage);
  folly::Optional<double> cpuPct{folly::none};
  if (prevCpuTime.timestamp != 0 && 
      nowCpuTime.timestamp > prevCpuTime.timestamp &&
      nowCpuTime.totalTime > prevCpuTime.totalTime) {
    uint64_t timestampDiff = nowCpuTime.timestamp - prevCpuTime.timestamp;
    uint64_t procTimeDiff = nowCpuTime.totalTime - prevCpuTime.totalTime;
    cpuPct = ((double)procTimeDiff / (double)timestampDiff) * 100;
  }
  prevCpuTime = nowCpuTime;
  return cpuPct;
}
folly :: Optional < double > SystemMetrics :: getCPUpercentage ( ) { struct rusage usage ; getrusage ( RUSAGE_SELF , & usage ) ; ProcCpuTime nowCpuTime ( usage ) ; folly :: Optional < double > cpuPct { folly :: none } ; if ( prevCpuTime . timestamp != 0 && nowCpuTime . timestamp > prevCpuTime . timestamp && nowCpuTime . totalTime > prevCpuTime . totalTime ) { uint64_t timestampDiff = nowCpuTime . timestamp - prevCpuTime . timestamp ; uint64_t procTimeDiff = nowCpuTime . totalTime - prevCpuTime . totalTime ; cpuPct = ( ( double ) procTimeDiff / ( double ) timestampDiff ) * 100 ; } prevCpuTime = nowCpuTime ; return cpuPct ; }
Return CPU% the process used
 This need to be called twice to get the time difference
 and calculate the CPU%.
Return CPU% the process used This need to be called twice to get the time difference and calculate the CPU% .
// calculate the CPU% = (process time diff) / (time elapsed) * 100 // has cached before // update the cache for next CPU% update