/src/platform_freebsd.cc

https://github.com/GunioRobot/node · C++ · 210 lines · 168 code · 41 blank · 1 comment · 19 complexity · 6a8bd6f6158002abcf413225ca1b81d2 MD5 · raw file

  1. #include "node.h"
  2. #include "platform.h"
  3. #include <v8.h>
  4. #include <stdlib.h>
  5. #include <kvm.h>
  6. #include <sys/param.h>
  7. #include <sys/sysctl.h>
  8. #include <sys/user.h>
  9. #include <sys/dkstat.h>
  10. #include <vm/vm_param.h>
  11. #include <string.h>
  12. #include <paths.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include <time.h>
  16. namespace node {
  17. using namespace v8;
  18. static char *process_title;
  19. char** Platform::SetupArgs(int argc, char *argv[]) {
  20. process_title = argc ? strdup(argv[0]) : NULL;
  21. return argv;
  22. }
  23. void Platform::SetProcessTitle(char *title) {
  24. if (process_title) free(process_title);
  25. process_title = strdup(title);
  26. setproctitle(title);
  27. }
  28. const char* Platform::GetProcessTitle(int *len) {
  29. if (process_title) {
  30. *len = strlen(process_title);
  31. return process_title;
  32. }
  33. *len = 0;
  34. return NULL;
  35. }
  36. int Platform::GetMemory(size_t *rss, size_t *vsize) {
  37. kvm_t *kd = NULL;
  38. struct kinfo_proc *kinfo = NULL;
  39. pid_t pid;
  40. int nprocs;
  41. size_t page_size = getpagesize();
  42. pid = getpid();
  43. kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
  44. if (kd == NULL) goto error;
  45. kinfo = kvm_getprocs(kd, KERN_PROC_PID, pid, &nprocs);
  46. if (kinfo == NULL) goto error;
  47. *rss = kinfo->ki_rssize * page_size;
  48. *vsize = kinfo->ki_size;
  49. kvm_close(kd);
  50. return 0;
  51. error:
  52. if (kd) kvm_close(kd);
  53. return -1;
  54. }
  55. int Platform::GetExecutablePath(char* buffer, size_t* size) {
  56. int mib[4];
  57. mib[0] = CTL_KERN;
  58. mib[1] = KERN_PROC;
  59. mib[2] = KERN_PROC_PATHNAME;
  60. mib[3] = -1;
  61. if (sysctl(mib, 4, buffer, size, NULL, 0) == -1) {
  62. return -1;
  63. }
  64. *size-=1;
  65. return 0;
  66. }
  67. int Platform::GetCPUInfo(Local<Array> *cpus) {
  68. Local<Object> cpuinfo;
  69. Local<Object> cputimes;
  70. unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
  71. multiplier = ((uint64_t)1000L / ticks), cpuspeed, maxcpus,
  72. cur = 0;
  73. char model[512];
  74. int numcpus;
  75. size_t size;
  76. size = sizeof(model);
  77. if (sysctlbyname("hw.model", &model, &size, NULL, 0) < 0) {
  78. return -1;
  79. }
  80. size = sizeof(numcpus);
  81. if (sysctlbyname("hw.ncpu", &numcpus, &size, NULL, 0) < 0) {
  82. return -1;
  83. }
  84. *cpus = Array::New(numcpus);
  85. size = sizeof(cpuspeed);
  86. if (sysctlbyname("hw.clockrate", &cpuspeed, &size, NULL, 0) < 0) {
  87. return -1;
  88. }
  89. // kern.cp_times on FreeBSD i386 gives an array up to maxcpus instead of ncpu
  90. size = sizeof(maxcpus);
  91. if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &size, NULL, 0) < 0) {
  92. return -1;
  93. }
  94. size = maxcpus * CPUSTATES * sizeof(long);
  95. long cp_times[size];
  96. if (sysctlbyname("kern.cp_times", &cp_times, &size, NULL, 0) < 0) {
  97. return -1;
  98. }
  99. for (int i = 0; i < numcpus; i++) {
  100. cpuinfo = Object::New();
  101. cputimes = Object::New();
  102. cputimes->Set(String::New("user"),
  103. Number::New((uint64_t)(cp_times[CP_USER+cur]) * multiplier));
  104. cputimes->Set(String::New("nice"),
  105. Number::New((uint64_t)(cp_times[CP_NICE+cur]) * multiplier));
  106. cputimes->Set(String::New("sys"),
  107. Number::New((uint64_t)(cp_times[CP_SYS+cur]) * multiplier));
  108. cputimes->Set(String::New("idle"),
  109. Number::New((uint64_t)(cp_times[CP_IDLE+cur]) * multiplier));
  110. cputimes->Set(String::New("irq"),
  111. Number::New((uint64_t)(cp_times[CP_INTR+cur]) * multiplier));
  112. cpuinfo->Set(String::New("model"), String::New(model));
  113. cpuinfo->Set(String::New("speed"), Number::New(cpuspeed));
  114. cpuinfo->Set(String::New("times"), cputimes);
  115. (*cpus)->Set(i, cpuinfo);
  116. cur+=CPUSTATES;
  117. }
  118. return 0;
  119. }
  120. double Platform::GetFreeMemory() {
  121. double pagesize = static_cast<double>(sysconf(_SC_PAGESIZE));
  122. unsigned long info;
  123. size_t size = sizeof(info);
  124. if (sysctlbyname("vm.stats.vm.v_free_count", &info, &size, NULL, 0) < 0) {
  125. return -1;
  126. }
  127. return (static_cast<double>(info)) * pagesize;
  128. }
  129. double Platform::GetTotalMemory() {
  130. #if defined(HW_PHYSMEM64)
  131. uint64_t info;
  132. static int which[] = {CTL_HW, HW_PHYSMEM64};
  133. #else
  134. unsigned int info;
  135. static int which[] = {CTL_HW, HW_PHYSMEM};
  136. #endif
  137. size_t size = sizeof(info);
  138. if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
  139. return -1;
  140. }
  141. return static_cast<double>(info);
  142. }
  143. double Platform::GetUptime() {
  144. time_t now;
  145. struct timeval info;
  146. size_t size = sizeof(info);
  147. static int which[] = {CTL_KERN, KERN_BOOTTIME};
  148. if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
  149. return -1;
  150. }
  151. now = time(NULL);
  152. return static_cast<double>(now - info.tv_sec);
  153. }
  154. int Platform::GetLoadAvg(Local<Array> *loads) {
  155. struct loadavg info;
  156. size_t size = sizeof(info);
  157. static int which[] = {CTL_VM, VM_LOADAVG};
  158. if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
  159. return -1;
  160. }
  161. (*loads)->Set(0, Number::New(static_cast<double>(info.ldavg[0])
  162. / static_cast<double>(info.fscale)));
  163. (*loads)->Set(1, Number::New(static_cast<double>(info.ldavg[1])
  164. / static_cast<double>(info.fscale)));
  165. (*loads)->Set(2, Number::New(static_cast<double>(info.ldavg[2])
  166. / static_cast<double>(info.fscale)));
  167. return 0;
  168. }
  169. } // namespace node