Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
getrusage.c
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2013-2014, 2016 Tobias Ellinghaus.
4 Copyright (C) 2016-2017 Peter Budai.
5 Copyright (C) 2020 Pascal Obry.
6 Copyright (C) 2022 Martin Baƙinka.
7
8 darktable is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 darktable is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with darktable. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22/*
23 * Copyright (c) 2014 Nicira, Inc.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at:
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
36 */
37
38#include "win/win.h"
39#include <config.h>
40#include <stdio.h>
41#include <psapi.h>
42#include "getrusage.h"
43
44static void usage_to_timeval(FILETIME *ft, struct timeval *tv)
45{
46 ULARGE_INTEGER time;
47 time.LowPart = ft->dwLowDateTime;
48 time.HighPart = ft->dwHighDateTime;
49
50 tv->tv_sec = time.QuadPart / 10000000;
51 tv->tv_usec = (time.QuadPart % 10000000) / 10;
52}
53
54int getrusage(int who, struct rusage *usage)
55{
56 FILETIME creation_time, exit_time, kernel_time, user_time;
57 PROCESS_MEMORY_COUNTERS pmc;
58
59 memset(usage, 0, sizeof(struct rusage));
60
61 if(who == RUSAGE_SELF)
62 {
63 if(!GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, &kernel_time, &user_time))
64 {
65 fprintf(stdout, "failed at GetProcessTimes\n");
66 return -1;
67 }
68
69 if(!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
70 {
71 fprintf(stdout, "failed at GetProcessMemoryInfo\n");
72 return -1;
73 }
74
75 usage_to_timeval(&kernel_time, &usage->ru_stime);
76 usage_to_timeval(&user_time, &usage->ru_utime);
77 usage->ru_majflt = pmc.PageFaultCount;
78 usage->ru_maxrss = pmc.PeakWorkingSetSize / 1024;
79 return 0;
80 }
81 else if(who == RUSAGE_THREAD)
82 {
83 if(!GetThreadTimes(GetCurrentThread(), &creation_time, &exit_time, &kernel_time, &user_time))
84 {
85 fprintf(stdout, "failed at GetThreadTimes\n");
86 return -1;
87 }
88 usage_to_timeval(&kernel_time, &usage->ru_stime);
89 usage_to_timeval(&user_time, &usage->ru_utime);
90 return 0;
91 }
92 else
93 {
94 return -1;
95 }
96}
97
98// clang-format off
99// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
100// vim: shiftwidth=2 expandtab tabstop=2 cindent
101// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
102// clang-format on
103
static void usage(const char *progname)
static void usage_to_timeval(FILETIME *ft, struct timeval *tv)
Definition getrusage.c:44
int getrusage(int who, struct rusage *usage)
Definition getrusage.c:54
#define RUSAGE_THREAD
Definition getrusage.h:62
#define RUSAGE_SELF
Definition getrusage.h:54