| 1 |
|
| /* $Id: Getrusage.xs 11 2006-02-04 00:53:56Z taffy $ -*- c -*- */ |
| 2 |
|
| |
| 3 |
|
| #include "EXTERN.h" |
| 4 |
|
| #include "perl.h" |
| 5 |
|
| #include "XSUB.h" |
| 6 |
|
| |
| 7 |
|
| #include "ppport.h" |
| 8 |
|
| |
| 9 |
|
| #include <string.h> |
| 10 |
|
| #include <stdio.h> |
| 11 |
|
| #include <assert.h> |
| 12 |
|
| #include <sys/types.h> |
| 13 |
|
| #include <sys/time.h> |
| 14 |
|
| #include <sys/resource.h> |
| 15 |
|
| |
| 16 |
|
| #define STORE_FIELD(format,data) string_store(hash, #data, format, res.data); |
| 17 |
|
| #define STORE_TIMEVAL(x) string_store(hash, #x, "%.6f", timeval_to_double(&res.x)); |
| 18 |
|
| #define syscall(x) ({ int y = (x); if(y < 0) { perror(#x); }; y; }) |
| 19 |
|
| |
| 20 |
0 |
| static void string_store(HV* hash, const char *key, const char *fmt, ...) { |
| 21 |
|
| char buffer[0x1000]; |
| 22 |
|
| va_list p; |
| 23 |
|
| int len; |
| 24 |
|
| |
| 25 |
|
| assert(hash != NULL); |
| 26 |
|
| assert(key != NULL); |
| 27 |
|
| assert(fmt != NULL); |
| 28 |
|
| |
| 29 |
0 |
| va_start(p, fmt); |
| 30 |
0 |
| len = vsnprintf(buffer, sizeof(buffer), fmt, p); |
| 31 |
0 |
| va_end(p); |
| 32 |
|
| |
| 33 |
0 |
| hv_store(hash, key, strlen(key), newSVpv(buffer, len), 0); |
| 34 |
0 |
| } |
| 35 |
|
| |
| 36 |
0 |
| static double timeval_to_double(const struct timeval *tv) { |
| 37 |
|
| assert(tv != NULL); |
| 38 |
|
| |
| 39 |
0 |
| return tv->tv_sec + tv->tv_usec * 1e-6; |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
0 |
| static void fetch_getrusage(HV *hash, int who) { |
| 43 |
|
| struct rusage res; |
| 44 |
|
| |
| 45 |
|
| assert(hash != NULL); |
| 46 |
|
| assert(who == RUSAGE_SELF || who == RUSAGE_CHILDREN); |
| 47 |
|
| |
| 48 |
0 |
▶ | if(syscall(getrusage(who, &res)) >= 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(syscall(getrusage(who, &res)) >= 0) { |
0 |
0 |
0 |
| if(syscall(getrusage(who, &res)) >= 0) { |
0 |
0 |
0 |
|
| 49 |
0 |
| STORE_TIMEVAL(ru_utime); /* leserlich etwas harmonieren^^ */ |
| 50 |
0 |
| STORE_TIMEVAL(ru_stime); |
| 51 |
|
| |
| 52 |
0 |
| STORE_FIELD("%ld", ru_maxrss); |
| 53 |
0 |
| STORE_FIELD("%ld", ru_ixrss); |
| 54 |
0 |
| STORE_FIELD("%ld", ru_idrss); |
| 55 |
0 |
| STORE_FIELD("%ld", ru_isrss); |
| 56 |
0 |
| STORE_FIELD("%ld", ru_minflt); |
| 57 |
0 |
| STORE_FIELD("%ld", ru_majflt); |
| 58 |
0 |
| STORE_FIELD("%ld", ru_nswap); |
| 59 |
0 |
| STORE_FIELD("%ld", ru_inblock); |
| 60 |
0 |
| STORE_FIELD("%ld", ru_oublock); |
| 61 |
0 |
| STORE_FIELD("%ld", ru_msgsnd); |
| 62 |
0 |
| STORE_FIELD("%ld", ru_msgrcv); |
| 63 |
0 |
| STORE_FIELD("%ld", ru_nsignals); |
| 64 |
0 |
| STORE_FIELD("%ld", ru_nvcsw); |
| 65 |
0 |
| STORE_FIELD("%ld", ru_nivcsw); |
| 66 |
|
| } |
| 67 |
0 |
| } |
| 68 |
|
| |
| 69 |
|
| MODULE = Unix::Getrusage PACKAGE = Unix::Getrusage |
| 70 |
|
| |
| 71 |
|
| SV* |
| 72 |
|
| getrusage() |
| 73 |
|
| PREINIT: |
| 74 |
|
| HV *hash; |
| 75 |
|
| CODE: |
| 76 |
0 |
| hash = newHV(); |
| 77 |
0 |
| fetch_getrusage(hash, RUSAGE_SELF); |
| 78 |
0 |
| sv_2mortal((SV*) hash); |
| 79 |
0 |
| RETVAL = newRV_inc((SV*)hash); |
| 80 |
|
| OUTPUT: |
| 81 |
|
| RETVAL |
| 82 |
|
| |
| 83 |
|
| SV* |
| 84 |
|
| getrusage_children() |
| 85 |
|
| PREINIT: |
| 86 |
|
| HV *hash; |
| 87 |
|
| CODE: |
| 88 |
0 |
| hash = newHV(); |
| 89 |
0 |
| fetch_getrusage(hash, RUSAGE_CHILDREN); |
| 90 |
0 |
| sv_2mortal((SV*) hash); |
| 91 |
0 |
| RETVAL = newRV_inc((SV*)hash); |
| 92 |
|
| OUTPUT: |
| 93 |
|
| RETVAL |