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
|
/*
* Copyright (C) 2000-2019, Thomas Maier-Komor
*
* This file is part of mbuffer's source code.
*
* This program 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, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mbconf.h"
#include "common.h"
#include "log.h"
#include "dest.h"
#include "globals.h"
#include "settings.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#if __STDC_VERSION__ < 199901L
#define restrict
#endif
#ifdef __sun
#if defined(__SunOS_5_8) || defined(__SunOS_5_9)
int SemWait(sema_t *s)
{
int err;
do {
err = sema_wait(s);
} while (err == EINTR);
return err;
}
#endif
#endif
static inline long long timediff(struct timespec *restrict t1, struct timespec *restrict t2)
{
long long tdiff;
tdiff = (t1->tv_sec - t2->tv_sec) * 1000000;
tdiff += (t1->tv_nsec - t2->tv_nsec) / 1000;
if (tdiff < 0) {
// Tdiff < 0 if no MONOTONIC clock and time adjust happend.
// Update time to current time to get a sane restarting
// point. Timing will transiently be incorrect.
tdiff = 0;
t2->tv_sec = t1->tv_sec;
t2->tv_nsec = t1->tv_nsec;
}
return tdiff;
}
/* Thread-safe replacement for usleep. Argument must be a whole
* number of microseconds to sleep.
*/
int mt_usleep(unsigned long long sleep_usecs)
{
struct timespec tv;
tv.tv_sec = sleep_usecs / 1000000;
tv.tv_nsec = (sleep_usecs % 1000000) * 1000;
do {
/* Sleep for the time specified in tv. If interrupted by a
* signal, place the remaining time left to sleep back into tv.
*/
if (0 == nanosleep(&tv, &tv))
return 0;
} while (errno == EINTR);
return -1;
}
long long enforceSpeedLimit(unsigned long long limit, long long num, struct timespec *last)
{
struct timespec now;
long long tdiff;
double dt;
long self = (long) pthread_self();
num += Blocksize;
if (num < 0) {
debugmsg("enforceSpeedLimit(%lld,%lld): thread %ld\n",limit,num,self);
return num;
}
(void) clock_gettime(ClockSrc,&now);
tdiff = timediff(&now,last);
dt = (double)tdiff * 1E-6;
if (((double)num/dt) > (double)limit) {
double req = (double)num/limit - dt;
long long w = (long long) (req * 1E6);
if (w >= TickTime) {
long long slept, ret;
(void) mt_usleep(w);
(void) clock_gettime(ClockSrc,last);
slept = timediff(last,&now);
ret = -(long long)((double)limit * (double)(slept-w) * 1E-6);
debugmsg("thread %ld: slept for %lld usec (planned for %lld), ret = %lld\n",self,slept,w,ret);
return ret;
} else {
debugmsg("thread %ld: request for sleeping %lld usec delayed\n",self,w);
/*
* Sleeping now would cause too much of a slowdown. So
* we defer this sleep until the sleeping time is
* longer than the tick time. Like this we can stay as
* close to the speed limit as possible.
*/
return num;
}
}
debugmsg("thread %ld: %lld/%g (%g) <= %g\n",self,num,dt,num/dt,(double)limit);
return num;
}
void releaseLock(void *l)
{
int err = pthread_mutex_unlock((pthread_mutex_t *)l);
assert(err == 0);
}
void enable_directio(int fd, const char *fn)
{
if (OptDirect == 0)
return;
#ifdef O_DIRECT
if (0 == fcntl(fd,F_SETFL,fcntl(fd,F_GETFL) | O_DIRECT))
infomsg("enabled O_DIRECT on %s\n",fn);
else
infomsg("could not enable O_DIRECT on %s\n",fn);
#endif
#ifdef __sun
if (-1 == directio(fd,DIRECTIO_ON))
infomsg("direct I/O hinting failed for output %s: %s\n",fn,strerror(errno));
else
infomsg("direct I/O hinting enabled for output to %s\n",fn);
#endif
}
int disable_directio(int fd, const char *fn)
{
#ifdef O_DIRECT
int fl = fcntl(fd,F_GETFL);
if ((fl & O_DIRECT) == 0) {
warningmsg("EINVAL without O_DIRECT on %s\n",fn);
return 0;
}
if (0 != fcntl(fd,F_SETFL,fl & ~O_DIRECT)) {
warningmsg("disabling O_DIRECT on %s failed with %s\n",fn,strerror(errno));
return 0;
}
infomsg("disabled O_DIRECT on %s\n",fn);
return 1;
#else
return 0;
#endif
}
// use only once per string formatting argument
const char *hBytes(unsigned long long v)
{
static char output[16];
char *dim = " kMGT";
while ((v != 0) && ((v % 1024) == 0)) {
++dim;
v >>= 10;
}
snprintf(output,sizeof(output),"%llu%ciB",v,*dim);
return output;
}
|