]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: cpu-limit - Split off cpu_limit_get_usage_msecs_with()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Mon, 15 Mar 2021 15:22:00 +0000 (17:22 +0200)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Tue, 16 Mar 2021 10:04:53 +0000 (10:04 +0000)
src/lib/cpu-limit.c

index 88d5189a8296546d4b65da9f6def283db983fcc1..2ad35a23572c432168ff42a5d017bd8646004cda 100644 (file)
@@ -38,20 +38,18 @@ cpu_limit_handler(const siginfo_t *si ATTR_UNUSED, void *context ATTR_UNUSED)
        }
 }
 
-unsigned int
-cpu_limit_get_usage_msecs(struct cpu_limit *climit, enum cpu_limit_type type)
+static unsigned int
+cpu_limit_get_usage_msecs_with(struct cpu_limit *climit,
+                              enum cpu_limit_type type,
+                              const struct rusage *rusage)
 {
-       struct rusage rusage;
        struct timeval cpu_usage = { 0, 0 };
        int usage_diff;
 
-       /* Query cpu usage so far */
-       if (getrusage(RUSAGE_SELF, &rusage) < 0)
-               i_fatal("getrusage() failed: %m");
        if ((type & CPU_LIMIT_TYPE_USER) != 0)
-               timeval_add(&cpu_usage, &rusage.ru_utime);
+               timeval_add(&cpu_usage, &rusage->ru_utime);
        if ((type & CPU_LIMIT_TYPE_SYSTEM) != 0)
-               timeval_add(&cpu_usage, &rusage.ru_stime);
+               timeval_add(&cpu_usage, &rusage->ru_stime);
 
        struct timeval initial_total = { 0, 0 };
        if ((type & CPU_LIMIT_TYPE_USER) != 0)
@@ -64,6 +62,18 @@ cpu_limit_get_usage_msecs(struct cpu_limit *climit, enum cpu_limit_type type)
        return (unsigned int)usage_diff;
 }
 
+unsigned int
+cpu_limit_get_usage_msecs(struct cpu_limit *climit, enum cpu_limit_type type)
+{
+       struct rusage rusage;
+
+       /* Query cpu usage so far */
+       if (getrusage(RUSAGE_SELF, &rusage) < 0)
+               i_fatal("getrusage() failed: %m");
+
+       return cpu_limit_get_usage_msecs_with(climit, type, &rusage);
+}
+
 #undef cpu_limit_init
 struct cpu_limit *
 cpu_limit_init(unsigned int cpu_limit_sec,