1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
/*
* Copyright (C) 2025 Jakub Kruszona-Zawadzki, Saglabs SA
*
* This file is part of MooseFS.
*
* MooseFS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 (only).
*
* MooseFS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MooseFS; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA
* or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if defined(GPERFTOOLS) && defined(HAVE_SETITIMER)
#undef HAVE_SETITIMER
#endif
#if defined(HAVE_SETITIMER)
# include <sys/time.h>
# ifndef ITIMER_REAL
# define ITIMER_REAL 0
# endif
# ifndef ITIMER_VIRTUAL
# define ITIMER_VIRTUAL 1
# endif
# ifndef ITIMER_PROF
# define ITIMER_PROF 2
# endif
# define MAXITIMER 999
#endif
#if defined(HAVE_GETRUSAGE)
# include <sys/types.h>
# ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
# endif
# ifdef HAVE_SYS_RUSAGE_H
# include <sys/rusage.h>
# endif
# ifndef RUSAGE_SELF
# define RUSAGE_SELF 0
# endif
#endif
#if defined(HAVE_GETTIMEOFDAY)
# include <sys/time.h>
#else
# include <time.h>
#endif
#include <stdlib.h>
#include <inttypes.h>
#if defined(HAVE_SETITIMER)
static struct itimerval it_set;
static uint32_t addsec,addusec;
#endif
#if defined(HAVE_GETRUSAGE)
static uint64_t lastautime;
static uint64_t lastastime;
#endif
static uint64_t lastget;
void cpu_init (void) {
#if defined(HAVE_SETITIMER)
struct itimerval rc,uc,pc;
#endif
#if defined(HAVE_GETRUSAGE)
struct rusage rus;
#endif
#if defined(HAVE_GETTIMEOFDAY)
struct timeval tod;
#endif
#if defined(HAVE_GETTIMEOFDAY)
gettimeofday(&tod,NULL);
lastget = tod.tv_sec;
lastget *= UINT64_C(1000000);
lastget += tod.tv_usec;
#else
lastget = time(NULL);
lastget *= UINT64_C(1000000);
#endif
#if defined(HAVE_SETITIMER)
addsec = 0;
addusec = 0;
it_set.it_interval.tv_sec = 0;
it_set.it_interval.tv_usec = 0;
it_set.it_value.tv_sec = MAXITIMER;
it_set.it_value.tv_usec = 999999;
setitimer(ITIMER_REAL,&it_set,&rc); // real time
setitimer(ITIMER_VIRTUAL,&it_set,&uc); // user time
setitimer(ITIMER_PROF,&it_set,&pc); // user time + system time
#endif
#if defined(HAVE_GETRUSAGE)
getrusage(RUSAGE_SELF,&rus);
lastautime = (uint64_t)rus.ru_utime.tv_sec;
lastautime *= UINT64_C(1000000);
lastautime += (uint64_t)rus.ru_utime.tv_usec;
lastastime = (uint64_t)rus.ru_stime.tv_sec;
lastastime *= UINT64_C(1000000);
lastastime += (uint64_t)rus.ru_stime.tv_usec;
#endif
}
void cpu_used (uint64_t *scpu,uint64_t *ucpu) {
#if defined(HAVE_SETITIMER)
struct itimerval rc,uc,pc;
uint64_t ucusec,pcusec;
#endif
#if defined(HAVE_GETRUSAGE)
struct rusage rus;
uint64_t autime,astime;
#endif
uint64_t rdiff,now;
#if defined(HAVE_GETTIMEOFDAY)
struct timeval tod;
#endif
uint64_t systime,usertime;
#if defined(HAVE_GETTIMEOFDAY)
gettimeofday(&tod,NULL);
now = tod.tv_sec;
now *= UINT64_C(1000000);
now += tod.tv_usec;
#else
now = time(NULL);
now *= UINT64_C(1000000);
#endif
if (now>lastget) {
rdiff = now-lastget;
lastget = now;
} else {
rdiff = 0;
}
#if !(defined(HAVE_SETITIMER) || defined(HAVE_GETRUSAGE))
systime = 0;
usertime = 0;
#endif
// mfs_log(MFSLOG_SYSLOG,MFSLOG_DEBUG,"rdiff1: %"PRIu64,rdiff);
#if defined(HAVE_SETITIMER)
/* method 1 - use itimers - usually worse */
setitimer(ITIMER_REAL,&it_set,&rc); // real time
setitimer(ITIMER_VIRTUAL,&it_set,&uc); // user time
setitimer(ITIMER_PROF,&it_set,&pc); // user time + system time
rc.it_value.tv_sec = MAXITIMER-rc.it_value.tv_sec;
rc.it_value.tv_usec = 999999-rc.it_value.tv_usec;
uc.it_value.tv_sec = MAXITIMER-uc.it_value.tv_sec;
uc.it_value.tv_usec = 999999-uc.it_value.tv_usec;
pc.it_value.tv_sec = MAXITIMER-pc.it_value.tv_sec;
pc.it_value.tv_usec = 999999-pc.it_value.tv_usec;
addsec += rc.it_value.tv_sec;
addusec += rc.it_value.tv_usec;
while (addusec>1000000) {
addusec-=1000000;
addsec++;
}
if (rc.it_value.tv_sec>=0 && rc.it_value.tv_usec>=0) {
rdiff = (uint64_t)rc.it_value.tv_sec;
rdiff *= UINT64_C(1000000);
rdiff += (uint64_t)rc.it_value.tv_usec;
}
if (uc.it_value.tv_sec>=0 && pc.it_value.tv_sec>=0 && uc.it_value.tv_usec>=0 && pc.it_value.tv_usec>=0) {
ucusec = (uint64_t)uc.it_value.tv_sec;
ucusec *= UINT64_C(1000000);
ucusec += (uint64_t)uc.it_value.tv_usec;
pcusec = (uint64_t)pc.it_value.tv_sec;
pcusec *= UINT64_C(1000000);
pcusec += (uint64_t)pc.it_value.tv_usec;
} else {
ucusec = 0;
pcusec = 0;
}
if (pcusec>ucusec) {
pcusec-=ucusec;
} else {
pcusec=0;
}
usertime = ucusec;
systime = pcusec;
#endif
// mfs_log(MFSLOG_SYSLOG,MFSLOG_DEBUG,"rdiff2: %"PRIu64,rdiff);
// mfs_log(MFSLOG_SYSLOG,MFSLOG_DEBUG,"usertime1: %"PRIu64",systime1: %"PRIu64,usertime,systime);
#if defined(HAVE_GETRUSAGE)
/* method 2 - use getrusage - usually better (if both are available, overwrites data set by itimer method) */
getrusage(RUSAGE_SELF,&rus);
autime = (uint64_t)rus.ru_utime.tv_sec;
autime *= UINT64_C(1000000);
autime += (uint64_t)rus.ru_utime.tv_usec;
astime = (uint64_t)rus.ru_stime.tv_sec;
astime *= UINT64_C(1000000);
astime += (uint64_t)rus.ru_stime.tv_usec;
if (autime>lastautime) {
usertime = autime - lastautime;
lastautime = autime;
}
if (astime>lastastime) {
systime = astime - lastastime;
lastastime = astime;
}
#endif
// mfs_log(MFSLOG_SYSLOG,MFSLOG_DEBUG,"usertime2: %"PRIu64",systime2: %"PRIu64,usertime,systime);
if (rdiff>0) {
*scpu = (systime*UINT64_C(1000000000)) / rdiff;
*ucpu = (usertime*UINT64_C(1000000000)) / rdiff;
} else {
*scpu = 0;
*ucpu = 0;
}
// mfs_log(MFSLOG_SYSLOG,MFSLOG_DEBUG,"scpu: %"PRIu32",ucpu: %"PRIu32,*scpu,*ucpu);
}
|