~yolanda.robla/ubuntu/trusty/nodejs/add_distribution

« back to all changes in this revision

Viewing changes to src/platform_openbsd.cc

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2013-08-14 00:16:46 UTC
  • mfrom: (7.1.40 sid)
  • Revision ID: package-import@ubuntu.com-20130814001646-bzlysfh8sd6mukbo
Tags: 0.10.15~dfsg1-4
* Update 2005 patch, adding a handful of tests that can fail on
  slow platforms.
* Add 1004 patch to fix test failures when writing NaN to buffer
  on mipsel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
22
 
#include "node.h"
23
 
#include "platform.h"
24
 
 
25
 
#include <v8.h>
26
 
 
27
 
#include <stdlib.h>
28
 
#include <kvm.h>
29
 
#include <sys/param.h>
30
 
#include <sys/sysctl.h>
31
 
#include <sys/user.h>
32
 
#include <sys/dkstat.h>
33
 
#include <uvm/uvm_param.h>
34
 
#include <string.h>
35
 
#include <paths.h>
36
 
#include <fcntl.h>
37
 
#include <unistd.h>
38
 
#include <time.h>
39
 
 
40
 
#include <stdio.h>
41
 
namespace node {
42
 
 
43
 
using namespace v8;
44
 
 
45
 
static char *process_title;
46
 
double Platform::prog_start_time = Platform::GetUptime();
47
 
 
48
 
char** Platform::SetupArgs(int argc, char *argv[]) {
49
 
  process_title = argc ? strdup(argv[0]) : NULL;
50
 
  return argv;
51
 
}
52
 
 
53
 
 
54
 
void Platform::SetProcessTitle(char *title) {
55
 
  if (process_title) free(process_title);
56
 
  process_title = strdup(title);
57
 
  setproctitle(title);
58
 
}
59
 
 
60
 
const char* Platform::GetProcessTitle(int *len) {
61
 
  if (process_title) {
62
 
    *len = strlen(process_title);
63
 
    return process_title;
64
 
  }
65
 
  *len = 0;
66
 
  return NULL;
67
 
}
68
 
 
69
 
int Platform::GetMemory(size_t *rss) {
70
 
  kvm_t *kd = NULL;
71
 
  struct kinfo_proc2 *kinfo = NULL;
72
 
  pid_t pid;
73
 
  int nprocs, max_size = sizeof(struct kinfo_proc2);
74
 
  size_t page_size = getpagesize();
75
 
 
76
 
  pid = getpid();
77
 
 
78
 
  kd = kvm_open(NULL, _PATH_MEM, NULL, O_RDONLY, "kvm_open");
79
 
  if (kd == NULL) goto error;
80
 
 
81
 
  kinfo = kvm_getproc2(kd, KERN_PROC_PID, pid, max_size, &nprocs);
82
 
  if (kinfo == NULL) goto error;
83
 
 
84
 
  *rss = kinfo->p_vm_rssize * page_size;
85
 
 
86
 
  kvm_close(kd);
87
 
 
88
 
  return 0;
89
 
 
90
 
error:
91
 
  if (kd) kvm_close(kd);
92
 
  return -1;
93
 
}
94
 
 
95
 
 
96
 
int Platform::GetCPUInfo(Local<Array> *cpus) {
97
 
  Local<Object> cpuinfo;
98
 
  Local<Object> cputimes;
99
 
  unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
100
 
               multiplier = ((uint64_t)1000L / ticks), cpuspeed;
101
 
  uint64_t info[CPUSTATES];
102
 
  char model[512];
103
 
  int numcpus = 1;
104
 
  static int which[] = {CTL_HW, HW_MODEL, NULL};
105
 
  size_t size;
106
 
 
107
 
  size = sizeof(model);
108
 
  if (sysctl(which, 2, &model, &size, NULL, 0) < 0) {
109
 
    return -1;
110
 
  }
111
 
  which[1] = HW_NCPU;
112
 
  size = sizeof(numcpus);
113
 
  if (sysctl(which, 2, &numcpus, &size, NULL, 0) < 0) {
114
 
    return -1;
115
 
  }
116
 
 
117
 
  *cpus = Array::New(numcpus);
118
 
 
119
 
  which[1] = HW_CPUSPEED;
120
 
  size = sizeof(cpuspeed);
121
 
  if (sysctl(which, 2, &cpuspeed, &size, NULL, 0) < 0) {
122
 
    return -1;
123
 
  }
124
 
  size = sizeof(info);
125
 
  which[0] = CTL_KERN;
126
 
  which[1] = KERN_CPTIME2;
127
 
  for (int i = 0; i < numcpus; i++) {
128
 
    which[2] = i;
129
 
    size = sizeof(info);
130
 
    if (sysctl(which, 3, &info, &size, NULL, 0) < 0) {
131
 
      return -1;
132
 
    }
133
 
    cpuinfo = Object::New();
134
 
    cputimes = Object::New();
135
 
    cputimes->Set(String::New("user"),
136
 
                  Number::New((uint64_t)(info[CP_USER]) * multiplier));
137
 
    cputimes->Set(String::New("nice"),
138
 
                  Number::New((uint64_t)(info[CP_NICE]) * multiplier));
139
 
    cputimes->Set(String::New("sys"),
140
 
                  Number::New((uint64_t)(info[CP_SYS]) * multiplier));
141
 
    cputimes->Set(String::New("idle"),
142
 
                  Number::New((uint64_t)(info[CP_IDLE]) * multiplier));
143
 
    cputimes->Set(String::New("irq"),
144
 
                  Number::New((uint64_t)(info[CP_INTR]) * multiplier));
145
 
 
146
 
    cpuinfo->Set(String::New("model"), String::New(model));
147
 
    cpuinfo->Set(String::New("speed"), Number::New(cpuspeed));
148
 
 
149
 
    cpuinfo->Set(String::New("times"), cputimes);
150
 
    (*cpus)->Set(i, cpuinfo);
151
 
  }
152
 
  return 0;
153
 
}
154
 
 
155
 
double Platform::GetUptimeImpl() {
156
 
  time_t now;
157
 
  struct timeval info;
158
 
  size_t size = sizeof(info);
159
 
  static int which[] = {CTL_KERN, KERN_BOOTTIME};
160
 
 
161
 
  if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
162
 
    return -1;
163
 
  }
164
 
  now = time(NULL);
165
 
 
166
 
  return static_cast<double>(now - info.tv_sec);
167
 
}
168
 
 
169
 
 
170
 
Handle<Value> Platform::GetInterfaceAddresses() {
171
 
  HandleScope scope;
172
 
  return scope.Close(Object::New());
173
 
}
174
 
 
175
 
 
176
 
}  // namespace node