1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
27 #include <sys/param.h> // for MAXPATHLEN
28 #include <sys/sysinfo.h>
29 #include <unistd.h> // getpagesize, sysconf
30 #include <stdio.h> // sscanf, snprintf
40 static char buf[MAXPATHLEN + 1];
41 static char *process_title = NULL;
42 double Platform::prog_start_time = Platform::GetUptime();
44 // Does the about the same as perror(), but for windows api functions
45 static void _winapi_perror(const char* prefix = NULL) {
46 DWORD errorno = GetLastError();
49 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
50 NULL, errorno, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
52 // FormatMessage messages include a newline character
55 fprintf(stderr, "%s: %s", prefix, errmsg);
57 fputs(errmsg, stderr);
62 char** Platform::SetupArgs(int argc, char *argv[]) {
67 void Platform::SetProcessTitle(char *title) {
68 // We need to convert _title_ to UTF-16 first, because that's what windows uses internally.
69 // It would be more efficient to use the UTF-16 value that we can obtain from v8,
70 // but it's not accessible from here.
72 // Max title length; according to the specs it should be 64K but in practice it's a little over 30000,
73 // but who needs titles that long anyway?
74 const int MAX_TITLE_LENGTH = 30001;
79 // Find out how big the buffer for the wide-char title must be
80 length = MultiByteToWideChar(CP_UTF8, 0, title, -1, NULL, 0);
82 _winapi_perror("MultiByteToWideChar");
86 // Convert to wide-char string
87 title_w = new WCHAR[length];
88 length = MultiByteToWideChar(CP_UTF8, 0, title, -1, title_w, length);
90 _winapi_perror("MultiByteToWideChar");
95 // If the title must be truncated insert a \0 terminator there
96 if (length > MAX_TITLE_LENGTH) {
97 title_w[MAX_TITLE_LENGTH - 1] = *L"\0";
100 if (!SetConsoleTitleW(title_w)) {
101 _winapi_perror("SetConsoleTitleW");
105 process_title = strdup(title);
111 static inline char* _getProcessTitle() {
114 int length, length_w;
116 length_w = GetConsoleTitleW((WCHAR*)L"\0", sizeof(WCHAR));
118 // If length is zero, there may be an error or the title may be empty
120 if (GetLastError()) {
121 _winapi_perror("GetConsoleTitleW");
124 // The title is empty, so return empty string
125 process_title = strdup("\0");
126 return process_title;
130 // Room for \0 terminator
133 title_w = new WCHAR[length_w];
135 if (!GetConsoleTitleW(title_w, length_w * sizeof(WCHAR))) {
136 _winapi_perror("GetConsoleTitleW");
141 // Find out what the size of the buffer is that we need
142 length = WideCharToMultiByte(CP_UTF8, 0, title_w, length_w, NULL, 0, NULL, NULL);
144 _winapi_perror("WideCharToMultiByte");
149 title = (char *) malloc(length);
156 // Do utf16 -> utf8 conversion here
157 if (!WideCharToMultiByte(CP_UTF8, 0, title_w, -1, title, length, NULL, NULL)) {
158 _winapi_perror("WideCharToMultiByte");
170 const char* Platform::GetProcessTitle(int *len) {
171 // If the process_title was never read before nor explicitly set,
172 // we must query it with getConsoleTitleW
173 if (!process_title) {
174 process_title = _getProcessTitle();
178 *len = strlen(process_title);
179 return process_title;
187 int Platform::GetMemory(size_t *rss, size_t *vsize) {
188 FILE *f = fopen("/proc/self/stat", "r");
193 size_t page_size = getpagesize();
196 if (fscanf(f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
198 if (fscanf (f, "%s ", buf) == 0) goto error; /* coverity[secure_coding] */
200 if (fscanf (f, "%c ", &ctmp) == 0) goto error; /* coverity[secure_coding] */
202 if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
204 if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
206 if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
208 if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
209 /* TTY owner process group */
210 if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
212 if (fscanf (f, "%u ", &itmp) == 0) goto error; /* coverity[secure_coding] */
213 /* Minor faults (no memory page) */
214 if (fscanf (f, "%u ", &itmp) == 0) goto error; /* coverity[secure_coding] */
215 /* Minor faults, children */
216 if (fscanf (f, "%u ", &itmp) == 0) goto error; /* coverity[secure_coding] */
217 /* Major faults (memory page faults) */
218 if (fscanf (f, "%u ", &itmp) == 0) goto error; /* coverity[secure_coding] */
219 /* Major faults, children */
220 if (fscanf (f, "%u ", &itmp) == 0) goto error; /* coverity[secure_coding] */
222 if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
224 if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
225 /* utime, children */
226 if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
227 /* stime, children */
228 if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
229 /* jiffies remaining in current time slice */
230 if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
232 if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
233 /* jiffies until next timeout */
234 if (fscanf (f, "%u ", &itmp) == 0) goto error; /* coverity[secure_coding] */
235 /* jiffies until next SIGALRM */
236 if (fscanf (f, "%u ", &itmp) == 0) goto error; /* coverity[secure_coding] */
237 /* start time (jiffies since system boot) */
238 if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
240 /* Virtual memory size */
241 if (fscanf (f, "%u ", &itmp) == 0) goto error; /* coverity[secure_coding] */
242 *vsize = (size_t) itmp;
244 /* Resident set size */
245 if (fscanf (f, "%u ", &itmp) == 0) goto error; /* coverity[secure_coding] */
246 *rss = (size_t) itmp * page_size;
249 if (fscanf (f, "%u ", &itmp) == 0) goto error; /* coverity[secure_coding] */
251 if (fscanf (f, "%u ", &itmp) == 0) goto error; /* coverity[secure_coding] */
253 if (fscanf (f, "%u ", &itmp) == 0) goto error; /* coverity[secure_coding] */
255 if (fscanf (f, "%u ", &itmp) == 0) goto error; /* coverity[secure_coding] */
267 int Platform::GetExecutablePath(char* buffer, size_t* size) {
268 *size = readlink("/proc/self/exe", buffer, *size - 1);
269 if (*size <= 0) return -1;
270 buffer[*size] = '\0';
274 int Platform::GetCPUInfo(Local<Array> *cpus) {
276 Local<Object> cpuinfo;
277 Local<Object> cputimes;
278 unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
279 multiplier = ((uint64_t)1000L / ticks), cpuspeed;
280 int numcpus = 0, i = 0;
281 unsigned long long ticks_user, ticks_sys, ticks_idle, ticks_nice, ticks_intr;
282 char line[512], speedPath[256], model[512];
283 FILE *fpStat = fopen("/proc/stat", "r");
284 FILE *fpModel = fopen("/proc/cpuinfo", "r");
288 while (fgets(line, 511, fpModel) != NULL) {
289 if (strncmp(line, "model name", 10) == 0) {
292 char *p = strchr(line, ':') + 2;
294 model[strlen(model)-1] = 0;
296 } else if (strncmp(line, "cpu MHz", 7) == 0) {
298 sscanf(line, "%*s %*s : %u", &cpuspeed);
305 *cpus = Array::New(numcpus);
308 while (fgets(line, 511, fpStat) != NULL) {
309 if (strncmp(line, "cpu ", 4) == 0) {
311 } else if (strncmp(line, "cpu", 3) != 0) {
315 sscanf(line, "%*s %llu %llu %llu %llu",
316 &ticks_user, &ticks_nice, &ticks_sys, &ticks_idle);
317 snprintf(speedPath, sizeof(speedPath),
318 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq", i);
320 fpSpeed = fopen(speedPath, "r");
322 if (fgets(line, 511, fpSpeed) != NULL) {
323 sscanf(line, "%u", &cpuspeed);
328 cpuinfo = Object::New();
329 cputimes = Object::New();
330 cputimes->Set(String::New("user"), Number::New(ticks_user * multiplier));
331 cputimes->Set(String::New("nice"), Number::New(ticks_nice * multiplier));
332 cputimes->Set(String::New("sys"), Number::New(ticks_sys * multiplier));
333 cputimes->Set(String::New("idle"), Number::New(ticks_idle * multiplier));
334 cputimes->Set(String::New("irq"), Number::New(0));
336 cpuinfo->Set(String::New("model"), String::New(model));
337 cpuinfo->Set(String::New("speed"), Number::New(cpuspeed));
339 cpuinfo->Set(String::New("times"), cputimes);
340 (*cpus)->Set(i++, cpuinfo);
348 double Platform::GetFreeMemory() {
349 double pagesize = static_cast<double>(sysconf(_SC_PAGESIZE));
350 double pages = static_cast<double>(sysconf(_SC_AVPHYS_PAGES));
352 return static_cast<double>(pages * pagesize);
355 double Platform::GetTotalMemory() {
356 double pagesize = static_cast<double>(sysconf(_SC_PAGESIZE));
357 double pages = static_cast<double>(sysconf(_SC_PHYS_PAGES));
359 return pages * pagesize;
362 double Platform::GetUptimeImpl() {
365 FILE *fpUptime = fopen("/proc/uptime", "r");
368 if (fgets(line, 511, fpUptime) != NULL) {
369 sscanf(line, "%lf %*lf", &amount);
377 int Platform::GetLoadAvg(Local<Array> *loads) {
378 // Unsupported as of cygwin 1.7.7