netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / getrusage-windows.c
1 /*
2  * Copyright (c) 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include <errno.h>
19 #include <psapi.h>
20 #include <sys/resource.h>
21 #include <time.h>
22 #include "util.h"
23 #include "openvswitch/vlog.h"
24
25 VLOG_DEFINE_THIS_MODULE(getrusage_windows);
26
27 static void
28 usage_to_timeval(FILETIME *ft, struct timeval *tv)
29 {
30     ULARGE_INTEGER time;
31     time.LowPart = ft->dwLowDateTime;
32     time.HighPart = ft->dwHighDateTime;
33
34     tv->tv_sec = time.QuadPart / 10000000;
35     tv->tv_usec = (time.QuadPart % 10000000) / 10;
36 }
37
38 int
39 getrusage(int who, struct rusage *usage)
40 {
41     FILETIME creation_time, exit_time, kernel_time, user_time;
42     PROCESS_MEMORY_COUNTERS pmc;
43
44     memset(usage, 0, sizeof(struct rusage));
45
46     if (who == RUSAGE_SELF) {
47         if (!GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time,
48                              &kernel_time, &user_time)) {
49             VLOG_ERR("failed at GetProcessTimes: %s",
50                       ovs_lasterror_to_string());
51             return -1;
52         }
53
54         if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
55             VLOG_ERR("failed at GetProcessMemoryInfo: %s",
56                       ovs_lasterror_to_string());
57             return -1;
58         }
59
60         usage_to_timeval(&kernel_time, &usage->ru_stime);
61         usage_to_timeval(&user_time, &usage->ru_utime);
62         usage->ru_majflt = pmc.PageFaultCount;
63         usage->ru_maxrss = pmc.PeakWorkingSetSize / 1024;
64         return 0;
65     } else if (who == RUSAGE_THREAD) {
66         if (!GetThreadTimes(GetCurrentThread(), &creation_time, &exit_time,
67                             &kernel_time, &user_time)) {
68             VLOG_ERR("failed at GetThreadTimes: %s",
69                       ovs_lasterror_to_string());
70             return -1;
71         }
72         usage_to_timeval(&kernel_time, &usage->ru_stime);
73         usage_to_timeval(&user_time, &usage->ru_utime);
74         return 0;
75     } else {
76         return -1;
77     }
78 }