PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/platform_openbsd.cc

https://github.com/rootslab/node
C++ | 177 lines | 125 code | 32 blank | 20 comment | 13 complexity | 37bdfc25ec17168e853dd217b68f8ab4 MD5 | raw file
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #include "node.h"
  22. #include "platform.h"
  23. #include <v8.h>
  24. #include <stdlib.h>
  25. #include <kvm.h>
  26. #include <sys/param.h>
  27. #include <sys/sysctl.h>
  28. #include <sys/user.h>
  29. #include <sys/dkstat.h>
  30. #include <uvm/uvm_param.h>
  31. #include <string.h>
  32. #include <paths.h>
  33. #include <fcntl.h>
  34. #include <unistd.h>
  35. #include <time.h>
  36. #include <stdio.h>
  37. namespace node {
  38. using namespace v8;
  39. static char *process_title;
  40. double Platform::prog_start_time = Platform::GetUptime();
  41. char** Platform::SetupArgs(int argc, char *argv[]) {
  42. process_title = argc ? strdup(argv[0]) : NULL;
  43. return argv;
  44. }
  45. void Platform::SetProcessTitle(char *title) {
  46. if (process_title) free(process_title);
  47. process_title = strdup(title);
  48. setproctitle(title);
  49. }
  50. const char* Platform::GetProcessTitle(int *len) {
  51. if (process_title) {
  52. *len = strlen(process_title);
  53. return process_title;
  54. }
  55. *len = 0;
  56. return NULL;
  57. }
  58. int Platform::GetMemory(size_t *rss, size_t *vsize) {
  59. kvm_t *kd = NULL;
  60. struct kinfo_proc2 *kinfo = NULL;
  61. pid_t pid;
  62. int nprocs, max_size = sizeof(struct kinfo_proc2);
  63. size_t page_size = getpagesize();
  64. pid = getpid();
  65. kd = kvm_open(NULL, _PATH_MEM, NULL, O_RDONLY, "kvm_open");
  66. if (kd == NULL) goto error;
  67. kinfo = kvm_getproc2(kd, KERN_PROC_PID, pid, max_size, &nprocs);
  68. if (kinfo == NULL) goto error;
  69. *rss = kinfo->p_vm_rssize * page_size;
  70. *vsize = kinfo->p_uru_ixrss;
  71. kvm_close(kd);
  72. return 0;
  73. error:
  74. if (kd) kvm_close(kd);
  75. return -1;
  76. }
  77. int Platform::GetCPUInfo(Local<Array> *cpus) {
  78. Local<Object> cpuinfo;
  79. Local<Object> cputimes;
  80. unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
  81. multiplier = ((uint64_t)1000L / ticks), cpuspeed;
  82. uint64_t info[CPUSTATES];
  83. char model[512];
  84. int numcpus = 1;
  85. static int which[] = {CTL_HW, HW_MODEL, NULL};
  86. size_t size;
  87. size = sizeof(model);
  88. if (sysctl(which, 2, &model, &size, NULL, 0) < 0) {
  89. return -1;
  90. }
  91. which[1] = HW_NCPU;
  92. size = sizeof(numcpus);
  93. if (sysctl(which, 2, &numcpus, &size, NULL, 0) < 0) {
  94. return -1;
  95. }
  96. *cpus = Array::New(numcpus);
  97. which[1] = HW_CPUSPEED;
  98. size = sizeof(cpuspeed);
  99. if (sysctl(which, 2, &cpuspeed, &size, NULL, 0) < 0) {
  100. return -1;
  101. }
  102. size = sizeof(info);
  103. which[0] = CTL_KERN;
  104. which[1] = KERN_CPTIME2;
  105. for (int i = 0; i < numcpus; i++) {
  106. which[2] = i;
  107. size = sizeof(info);
  108. if (sysctl(which, 3, &info, &size, NULL, 0) < 0) {
  109. return -1;
  110. }
  111. cpuinfo = Object::New();
  112. cputimes = Object::New();
  113. cputimes->Set(String::New("user"),
  114. Number::New((uint64_t)(info[CP_USER]) * multiplier));
  115. cputimes->Set(String::New("nice"),
  116. Number::New((uint64_t)(info[CP_NICE]) * multiplier));
  117. cputimes->Set(String::New("sys"),
  118. Number::New((uint64_t)(info[CP_SYS]) * multiplier));
  119. cputimes->Set(String::New("idle"),
  120. Number::New((uint64_t)(info[CP_IDLE]) * multiplier));
  121. cputimes->Set(String::New("irq"),
  122. Number::New((uint64_t)(info[CP_INTR]) * multiplier));
  123. cpuinfo->Set(String::New("model"), String::New(model));
  124. cpuinfo->Set(String::New("speed"), Number::New(cpuspeed));
  125. cpuinfo->Set(String::New("times"), cputimes);
  126. (*cpus)->Set(i, cpuinfo);
  127. }
  128. return 0;
  129. }
  130. double Platform::GetUptimeImpl() {
  131. time_t now;
  132. struct timeval info;
  133. size_t size = sizeof(info);
  134. static int which[] = {CTL_KERN, KERN_BOOTTIME};
  135. if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
  136. return -1;
  137. }
  138. now = time(NULL);
  139. return static_cast<double>(now - info.tv_sec);
  140. }
  141. Handle<Value> Platform::GetInterfaceAddresses() {
  142. HandleScope scope;
  143. return scope.Close(Object::New());
  144. }
  145. } // namespace node