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 <errno.h>
42#include <psapi.h>
43#include <sys/time.h>
44#include "getrusage.h"
45
46static void usage_to_timeval(FILETIME *ft, struct timeval *tv)
47{
48 ULARGE_INTEGER time;
49 time.LowPart = ft->dwLowDateTime;
50 time.HighPart = ft->dwHighDateTime;
51
52 tv->tv_sec = time.QuadPart / 10000000;
53 tv->tv_usec = (time.QuadPart % 10000000) / 10;
54}
55
56int getrusage(int who, struct rusage *usage)
57{
58 FILETIME creation_time, exit_time, kernel_time, user_time;
59 PROCESS_MEMORY_COUNTERS pmc;
60
61 memset(usage, 0, sizeof(struct rusage));
62
63 if(who == RUSAGE_SELF)
64 {
65 if(!GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, &kernel_time, &user_time))
66 {
67 fprintf(stdout, "failed at GetProcessTimes\n");
68 return -1;
69 }
70
71 if(!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
72 {
73 fprintf(stdout, "failed at GetProcessMemoryInfo\n");
74 return -1;
75 }
76
77 usage_to_timeval(&kernel_time, &usage->ru_stime);
78 usage_to_timeval(&user_time, &usage->ru_utime);
79 usage->ru_majflt = pmc.PageFaultCount;
80 usage->ru_maxrss = pmc.PeakWorkingSetSize / 1024;
81 return 0;
82 }
83 else if(who == RUSAGE_THREAD)
84 {
85 if(!GetThreadTimes(GetCurrentThread(), &creation_time, &exit_time, &kernel_time, &user_time))
86 {
87 fprintf(stdout, "failed at GetThreadTimes\n");
88 return -1;
89 }
90 usage_to_timeval(&kernel_time, &usage->ru_stime);
91 usage_to_timeval(&user_time, &usage->ru_utime);
92 return 0;
93 }
94 else
95 {
96 return -1;
97 }
98}
99
100// clang-format off
101// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
102// vim: shiftwidth=2 expandtab tabstop=2 cindent
103// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
104// clang-format on
105
static void usage(const char *progname)
Definition cli/main.c:87
static void usage_to_timeval(FILETIME *ft, struct timeval *tv)
Definition getrusage.c:46
int getrusage(int who, struct rusage *usage)
Definition getrusage.c:56
#define RUSAGE_THREAD
Definition getrusage.h:61
#define RUSAGE_SELF
Definition getrusage.h:53