pl_util.c

filter: Statementstmt 86.6% 26 / 30 Branchbran 58.3% 7 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.5% 33 / 42 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« pl_module.c ↑ Summary pl_eventloop.c »
1 #include <stdio.h>
2 #include <sys/time.h>
3 #include "duk_console.h"
4 #include "pl_util.h"
5  
6 #define FILE_MEMORY_STATUS "/proc/self/statm"
7  
8 4002003 double now_us(void)
9 {
10 struct timeval tv;
11 4002003 double now = 0.0;
12 4002003 int rc = gettimeofday(&tv, 0);
13 4002003 ▶ if (rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) { 4002003 0 4002003
14 4002003 now = 1000000.0 * tv.tv_sec + tv.tv_usec;
15 }
16 4002003 return now;
17 }
18  
19 820 long total_memory_pages(void)
20 {
21 820 long pages = 0;
22  
23 /*
24 * /proc/[pid]/statm
25 * Provides information about memory usage, measured in pages.
26 * size total program size
27 * (same as VmSize in /proc/[pid]/status)
28 * resident resident set size
29 * (same as VmRSS in /proc/[pid]/status)
30 * share shared pages (from shared mappings)
31 * text text (code)
32 * lib library (unused in Linux 2.6)
33 * data data + stack
34 * dirty dirty pages (unused in Linux 2.6)
35 */
36 820 FILE* fp = 0;
37 do {
38 long size, resident, share, text, lib, data, dpages;
39 int nread;
40 820 fp = fopen(FILE_MEMORY_STATUS, "r");
41 820 ▶ if (!fp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) { 0 820 820
42 /* silently ignore, some OSs do not have this file */
43 0 break;
44 }
45 820 nread = fscanf(fp, "%ld %ld %ld %ld %ld %ld %ld",
46 &size, &resident, &share, &text, &lib, &data, &dpages);
47 820 ▶ if (nread != 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (nread != 7) { 0 820 820
48 /* silently ignore, avoid noisy errors */
49 0 break;
50 }
51 820 pages = size;
52 } while (0);
53 820 ▶ if (fp) {
Branch: 1 branch
BranchTrueFalseTotal
if (fp) { 820 0 820
54 820 fclose(fp);
55 820 fp = 0;
56 }
57 820 return pages;
58 }
59  
60 4001141 int check_duktape_call_for_errors(int rc, duk_context* ctx)
61 {
62 4001141 ▶ if (rc == DUK_EXEC_SUCCESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == DUK_EXEC_SUCCESS) { 4001129 12 4001141
63 /* no error */
64 4001129 return 1;
65 }
66  
67 12 ▶ if (duk_is_error(ctx, -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (duk_is_error(ctx, -1)) { 12 0 12
68 /*
69 * Error and we should have a stack trace.
70 * Accessing .stack might cause an error to be thrown, so wrap this
71 * access in a duk_safe_call() if it matters.
72 */
73 12 duk_get_prop_string(ctx, -1, "stack");
74 12 duk_console_log(ctx,DUK_CONSOLE_FLUSH | DUK_CONSOLE_TO_STDERR,
75 "error: %s\n", duk_safe_to_string(ctx, -1));
76 12 duk_pop(ctx);
77 12 return 0;
78 }
79  
80 /*
81 * Error without a stack trace.
82 * Non-Error value, coerce safely to string.
83 */
84 0 duk_console_log(ctx,DUK_CONSOLE_FLUSH | DUK_CONSOLE_TO_STDERR,
85 "error: %s\n", duk_safe_to_string(ctx, -1));
86 0 return 1;
87 }
« pl_module.c ↑ Summary pl_eventloop.c »