utils.h 3.26 KB
Newer Older
Mike Hibler's avatar
Mike Hibler committed
1
/*
2
 * Copyright (c) 2000-2017 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file 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 Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
Mike Hibler's avatar
Mike Hibler committed
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
 */

#include <time.h>

static inline int
pasttime(struct timeval *cur, struct timeval *next)
{
	return (cur->tv_sec > next->tv_sec ||
		(cur->tv_sec == next->tv_sec &&
		 cur->tv_usec >= next->tv_usec));
}

static inline void
addtime(struct timeval *next, struct timeval *cur, struct timeval *inc)
{
	next->tv_sec = cur->tv_sec + inc->tv_sec;
	next->tv_usec = cur->tv_usec + inc->tv_usec;
	if (next->tv_usec >= 1000000) {
		next->tv_usec -= 1000000;
		next->tv_sec++;
	}
}

static inline void
subtime(struct timeval *next, struct timeval *cur, struct timeval *dec)
{
	if (cur->tv_usec < dec->tv_usec) {
		next->tv_usec = (cur->tv_usec + 1000000) - dec->tv_usec;
		next->tv_sec = (cur->tv_sec - 1) - dec->tv_sec;
	} else {
		next->tv_usec = cur->tv_usec - dec->tv_usec;
		next->tv_sec = cur->tv_sec - dec->tv_sec;
	}
}

static inline void
addusec(struct timeval *next, struct timeval *cur, unsigned long usec)
{
	next->tv_sec = cur->tv_sec;
	next->tv_usec = cur->tv_usec + usec;
	while (next->tv_usec >= 1000000) {
		next->tv_usec -= 1000000;
		next->tv_sec++;
	}
}

/* Prototypes */
char   *CurrentTimeString(void);
int	sleeptime(unsigned int usecs, char *str, int doround);
int	fsleep(unsigned int usecs);
int	sleeptil(struct timeval *nexttime);
73 74 75 76 77 78
void	InitSizes(int32_t chunksize, int32_t blocksize, int64_t bytes);
int	TotalChunks(void);
int	ChunkSize(int chunkno);
int	ChunkBytes(int chunkno);
int	TotalBlocks(void);
int	BlockSize(int chunkno, int blockno);
Mike Hibler's avatar
Mike Hibler committed
79 80
void	BlockMapInit(BlockMap_t *blockmap, int block, int count);
void	BlockMapAdd(BlockMap_t *blockmap, int block, int count);
81
void	BlockMapClear(BlockMap_t *blockmap, int block, int count);
Mike Hibler's avatar
Mike Hibler committed
82 83 84 85 86 87 88
int	BlockMapAlloc(BlockMap_t *blockmap, int block);
int	BlockMapIsAlloc(BlockMap_t *blockmap, int block, int count);
int	BlockMapExtract(BlockMap_t *blockmap, int *blockp);
void	BlockMapInvert(BlockMap_t *oldmap, BlockMap_t *newmap);
int	BlockMapMerge(BlockMap_t *frommap, BlockMap_t *tomap);
int	BlockMapFirst(BlockMap_t *blockmap);
int	BlockMapApply(BlockMap_t *blockmap, int chunk,
Mike Hibler's avatar
Mike Hibler committed
89
		      int (*func)(int, int, int, void *), void *farg);
Mike Hibler's avatar
Mike Hibler committed
90
void	ClientStatsDump(unsigned int id, ClientStats_t *stats);
91 92 93
#ifdef EMULAB_EVENTS
char	*extract_imageid(char *opath);
#endif
94 95 96
#ifdef MASTER_SERVER
char	*GetMSError(int error);
char	*GetMSMethods(int methods);
97
void	PrintGetInfo(char *imageid, GetReply *reply, int raw);
98
void	PrintPutInfo(char *imageid, PutReply *reply, int raw);
99
#endif
100 101 102 103

/* Compat */
#define CHUNKSIZE	ChunkSize(-1)
#define BLOCKSIZE	BlockSize(-1, -1)