| 1 |
/* The MIT License |
| 2 |
|
| 3 |
Copyright (c) 2008 Genome Research Ltd (GRL). |
| 4 |
|
| 5 |
Permission is hereby granted, free of charge, to any person obtaining |
| 6 |
a copy of this software and associated documentation files (the |
| 7 |
"Software"), to deal in the Software without restriction, including |
| 8 |
without limitation the rights to use, copy, modify, merge, publish, |
| 9 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 10 |
permit persons to whom the Software is furnished to do so, subject to |
| 11 |
the following conditions: |
| 12 |
|
| 13 |
The above copyright notice and this permission notice shall be |
| 14 |
included in all copies or substantial portions of the Software. |
| 15 |
|
| 16 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 |
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 |
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
/* Contact: Heng Li <[email protected]> */ |
| 27 |
#define FSYNC_ON_FLUSH |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <stdarg.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <string.h> |
| 33 |
#include <zlib.h> |
| 34 |
#include <errno.h> |
| 35 |
#ifdef FSYNC_ON_FLUSH |
| 36 |
#include <sys/types.h> |
| 37 |
#include <sys/stat.h> |
| 38 |
#include <unistd.h> |
| 39 |
#endif |
| 40 |
#include <sys/resource.h> |
| 41 |
#include <sys/time.h> |
| 42 |
#include <time.h> |
| 43 |
#include "utils.h" |
| 44 |
|
| 45 |
#include "ksort.h" |
| 46 |
#define pair64_lt(a, b) ((a).x < (b).x || ((a).x == (b).x && (a).y < (b).y)) |
| 47 |
#define ks_heapdown_128 abpoa_ks_heapdown_128 |
| 48 |
#define ks_heapmake_128 abpoa_ks_heapmake_128 |
| 49 |
#define ks_ksmall_128 abpoa_ks_ksmall_128 |
| 50 |
KSORT_INIT(128, pair64_t, pair64_lt) |
| 51 |
#define ks_heapdown_64 abpoa_ks_heapdown_64 |
| 52 |
#define ks_heapmake_64 abpoa_ks_heapmake_64 |
| 53 |
#define ks_ksmall_64 abpoa_ks_ksmall_64 |
| 54 |
KSORT_INIT(64, uint64_t, ks_lt_generic) |
| 55 |
|
| 56 |
#include "kseq.h" |
| 57 |
#define kseq_init abpoa_kseq_init |
| 58 |
#define kseq_destroy abpoa_kseq_destroy |
| 59 |
#define kseq_read abpoa_kseq_read |
| 60 |
#define kseq_copy abpoa_kseq_copy |
| 61 |
KSEQ_INIT2(, gzFile, err_gzread) |
| 62 |
|
| 63 |
/******************** |
| 64 |
* System utilities * |
| 65 |
********************/ |
| 66 |
|
| 67 |
FILE *err_xopen_core(const char *func, const char *fn, const char *mode) |
| 68 |
{ |
| 69 |
FILE *fp = 0; |
| 70 |
if (strcmp(fn, "-") == 0) |
| 71 |
return (strstr(mode, "r"))? stdin : stdout; |
| 72 |
if ((fp = fopen(fn, mode)) == 0) { |
| 73 |
err_fatal(func, "fail to open file '%s' : %s", fn, strerror(errno)); |
| 74 |
} |
| 75 |
return fp; |
| 76 |
} |
| 77 |
|
| 78 |
FILE *err_xreopen_core(const char *func, const char *fn, const char *mode, FILE *fp) |
| 79 |
{ |
| 80 |
if (freopen(fn, mode, fp) == 0) { |
| 81 |
err_fatal(func, "fail to open file '%s' : %s", fn, strerror(errno)); |
| 82 |
} |
| 83 |
return fp; |
| 84 |
} |
| 85 |
|
| 86 |
gzFile err_xzopen_core(const char *func, const char *fn, const char *mode) |
| 87 |
{ |
| 88 |
gzFile fp; |
| 89 |
if (strcmp(fn, "-") == 0) { |
| 90 |
fp = gzdopen(fileno((strstr(mode, "r"))? stdin : stdout), mode); |
| 91 |
/* According to zlib.h, this is the only reason gzdopen can fail */ |
| 92 |
if (!fp) err_fatal(func, "Out of memory"); |
| 93 |
return fp; |
| 94 |
} |
| 95 |
if ((fp = gzopen(fn, mode)) == 0) { |
| 96 |
err_fatal(func, "fail to open file '%s' : %s", fn, errno ? strerror(errno) : "Out of memory"); |
| 97 |
} |
| 98 |
return fp; |
| 99 |
} |
| 100 |
|
| 101 |
void err_fatal(const char *header, const char *fmt, ...) |
| 102 |
{ |
| 103 |
va_list args; |
| 104 |
va_start(args, fmt); |
| 105 |
fprintf(stderr, "[%s] ", header); |
| 106 |
vfprintf(stderr, fmt, args); |
| 107 |
fprintf(stderr, "\n"); |
| 108 |
va_end(args); |
| 109 |
exit(EXIT_FAILURE); |
| 110 |
} |
| 111 |
|
| 112 |
void err_fatal_core(const char *header, const char *fmt, ...) |
| 113 |
{ |
| 114 |
va_list args; |
| 115 |
va_start(args, fmt); |
| 116 |
fprintf(stderr, "[%s] ", header); |
| 117 |
vfprintf(stderr, fmt, args); |
| 118 |
fprintf(stderr, " Abort!\n"); |
| 119 |
va_end(args); |
| 120 |
abort(); |
| 121 |
} |
| 122 |
|
| 123 |
void _err_fatal_simple(const char *func, const char *msg) |
| 124 |
{ |
| 125 |
fprintf(stderr, "[%s] %s\n", func, msg); |
| 126 |
exit(EXIT_FAILURE); |
| 127 |
} |
| 128 |
|
| 129 |
void _err_fatal_simple_core(const char *func, const char *msg) |
| 130 |
{ |
| 131 |
fprintf(stderr, "[%s] %s Abort!\n", func, msg); |
| 132 |
abort(); |
| 133 |
} |
| 134 |
|
| 135 |
size_t err_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) |
| 136 |
{ |
| 137 |
size_t ret = fwrite(ptr, size, nmemb, stream); |
| 138 |
if (ret != nmemb) |
| 139 |
_err_fatal_simple("fwrite", strerror(errno)); |
| 140 |
return ret; |
| 141 |
} |
| 142 |
|
| 143 |
size_t err_fread_noeof(void *ptr, size_t size, size_t nmemb, FILE *stream) |
| 144 |
{ |
| 145 |
size_t ret = fread(ptr, size, nmemb, stream); |
| 146 |
if (ret != nmemb) |
| 147 |
{ |
| 148 |
_err_fatal_simple("fread", ferror(stream) ? strerror(errno) : "Unexpected end of file"); |
| 149 |
} |
| 150 |
return ret; |
| 151 |
} |
| 152 |
|
| 153 |
int err_gzread(gzFile file, void *ptr, unsigned int len) |
| 154 |
{ |
| 155 |
int ret = gzread(file, ptr, len); |
| 156 |
|
| 157 |
if (ret < 0) |
| 158 |
{ |
| 159 |
int errnum = 0; |
| 160 |
const char *msg = gzerror(file, &errnum); |
| 161 |
_err_fatal_simple("gzread", Z_ERRNO == errnum ? strerror(errno) : msg); |
| 162 |
} |
| 163 |
|
| 164 |
return ret; |
| 165 |
} |
| 166 |
|
| 167 |
int err_fseek(FILE *stream, long offset, int whence) |
| 168 |
{ |
| 169 |
int ret = fseek(stream, offset, whence); |
| 170 |
if (0 != ret) |
| 171 |
{ |
| 172 |
_err_fatal_simple("fseek", strerror(errno)); |
| 173 |
} |
| 174 |
return ret; |
| 175 |
} |
| 176 |
|
| 177 |
long err_ftell(FILE *stream) |
| 178 |
{ |
| 179 |
long ret = ftell(stream); |
| 180 |
if (-1 == ret) |
| 181 |
{ |
| 182 |
_err_fatal_simple("ftell", strerror(errno)); |
| 183 |
} |
| 184 |
return ret; |
| 185 |
} |
| 186 |
|
| 187 |
int err_func_printf(const char *func, const char *format, ...) |
| 188 |
{ |
| 189 |
fprintf(stderr, "[%s] ", func); |
| 190 |
va_list arg; |
| 191 |
int done; |
| 192 |
va_start(arg, format); |
| 193 |
done = vfprintf(stderr, format, arg); |
| 194 |
fprintf(stderr, "\n"); |
| 195 |
int saveErrno = errno; |
| 196 |
va_end(arg); |
| 197 |
if (done < 0) _err_fatal_simple("vfprintf(stderr)", strerror(saveErrno)); |
| 198 |
return done; |
| 199 |
} |
| 200 |
|
| 201 |
int err_printf(const char *format, ...) |
| 202 |
{ |
| 203 |
va_list arg; |
| 204 |
int done; |
| 205 |
va_start(arg, format); |
| 206 |
done = vfprintf(stderr, format, arg); |
| 207 |
int saveErrno = errno; |
| 208 |
va_end(arg); |
| 209 |
if (done < 0) _err_fatal_simple("vfprintf(stderr)", strerror(saveErrno)); |
| 210 |
return done; |
| 211 |
} |
| 212 |
|
| 213 |
int stdout_printf(const char *format, ...) |
| 214 |
{ |
| 215 |
va_list arg; |
| 216 |
int done; |
| 217 |
va_start(arg, format); |
| 218 |
done = vfprintf(stdout, format, arg); |
| 219 |
int saveErrno = errno; |
| 220 |
va_end(arg); |
| 221 |
if (done < 0) _err_fatal_simple("vfprintf(stdout)", strerror(saveErrno)); |
| 222 |
return done; |
| 223 |
} |
| 224 |
|
| 225 |
int err_fprintf(FILE *stream, const char *format, ...) |
| 226 |
{ |
| 227 |
va_list arg; |
| 228 |
int done; |
| 229 |
va_start(arg, format); |
| 230 |
done = vfprintf(stream, format, arg); |
| 231 |
int saveErrno = errno; |
| 232 |
va_end(arg); |
| 233 |
if (done < 0) _err_fatal_simple("vfprintf", strerror(saveErrno)); |
| 234 |
return done; |
| 235 |
} |
| 236 |
|
| 237 |
int err_fputc(int c, FILE *stream) |
| 238 |
{ |
| 239 |
int ret = putc(c, stream); |
| 240 |
if (EOF == ret) |
| 241 |
{ |
| 242 |
_err_fatal_simple("fputc", strerror(errno)); |
| 243 |
} |
| 244 |
|
| 245 |
return ret; |
| 246 |
} |
| 247 |
|
| 248 |
int err_fputs(const char *s, FILE *stream) |
| 249 |
{ |
| 250 |
int ret = fputs(s, stream); |
| 251 |
if (EOF == ret) |
| 252 |
{ |
| 253 |
_err_fatal_simple("fputs", strerror(errno)); |
| 254 |
} |
| 255 |
|
| 256 |
return ret; |
| 257 |
} |
| 258 |
|
| 259 |
void err_fgets(char *buff, size_t s, FILE *fp) |
| 260 |
{ |
| 261 |
if (fgets(buff, s, fp) == NULL) { |
| 262 |
err_fatal_simple("fgets error.\n"); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
int err_puts(const char *s) |
| 267 |
{ |
| 268 |
int ret = puts(s); |
| 269 |
if (EOF == ret) |
| 270 |
{ |
| 271 |
_err_fatal_simple("puts", strerror(errno)); |
| 272 |
} |
| 273 |
|
| 274 |
return ret; |
| 275 |
} |
| 276 |
|
| 277 |
int err_fflush(FILE *stream) |
| 278 |
{ |
| 279 |
int ret = fflush(stream); |
| 280 |
if (ret != 0) _err_fatal_simple("fflush", strerror(errno)); |
| 281 |
|
| 282 |
#ifdef FSYNC_ON_FLUSH |
| 283 |
/* Calling fflush() ensures that all the data has made it to the |
| 284 |
kernel buffers, but this may not be sufficient for remote filesystems |
| 285 |
(e.g. NFS, lustre) as an error may still occur while the kernel |
| 286 |
is copying the buffered data to the file server. To be sure of |
| 287 |
catching these errors, we need to call fsync() on the file |
| 288 |
descriptor, but only if it is a regular file. */ |
| 289 |
{ |
| 290 |
struct stat sbuf; |
| 291 |
if (0 != fstat(fileno(stream), &sbuf)) |
| 292 |
_err_fatal_simple("fstat", strerror(errno)); |
| 293 |
|
| 294 |
if (S_ISREG(sbuf.st_mode)) |
| 295 |
{ |
| 296 |
if (0 != fsync(fileno(stream))) |
| 297 |
_err_fatal_simple("fsync", strerror(errno)); |
| 298 |
} |
| 299 |
} |
| 300 |
#endif |
| 301 |
return ret; |
| 302 |
} |
| 303 |
|
| 304 |
int err_fclose(FILE *stream) |
| 305 |
{ |
| 306 |
int ret = fclose(stream); |
| 307 |
if (ret != 0) _err_fatal_simple("fclose", strerror(errno)); |
| 308 |
return ret; |
| 309 |
} |
| 310 |
|
| 311 |
int err_gzclose(gzFile file) |
| 312 |
{ |
| 313 |
int ret = gzclose(file); |
| 314 |
if (Z_OK != ret) |
| 315 |
{ |
| 316 |
_err_fatal_simple("gzclose", Z_ERRNO == ret ? strerror(errno) : zError(ret)); |
| 317 |
} |
| 318 |
|
| 319 |
return ret; |
| 320 |
} |
| 321 |
|
| 322 |
/********* |
| 323 |
* alloc * |
| 324 |
*********/ |
| 325 |
void *err_malloc(const char *func, size_t s) |
| 326 |
{ |
| 327 |
void *ret = (void*)malloc(s); |
| 328 |
if (ret == NULL) err_fatal_core(func, "Malloc fail!\nSize: %lld\n", s); |
| 329 |
else return ret; |
| 330 |
} |
| 331 |
|
| 332 |
void *err_calloc(const char *func, size_t n, size_t s) |
| 333 |
{ |
| 334 |
void *ret = (void*)calloc(n, s); |
| 335 |
if (ret == NULL) err_fatal_core(func, "Calloc fail!\nN: %d\tSize: %lld\n", n, s); |
| 336 |
else return ret; |
| 337 |
} |
| 338 |
|
| 339 |
void *err_realloc(const char *func, void *p, size_t s) |
| 340 |
{ |
| 341 |
void *ret = (void*)realloc(p, s); |
| 342 |
if (ret == NULL) err_fatal_core(func, "Realloc fail!\nSize: %lld\n", s); |
| 343 |
else return ret; |
| 344 |
} |
| 345 |
|
| 346 |
/********* |
| 347 |
* Timer * |
| 348 |
*********/ |
| 349 |
void usr_sys_cputime(double *usr_t, double *sys_t) |
| 350 |
{ |
| 351 |
struct rusage r; |
| 352 |
getrusage(RUSAGE_SELF, &r); |
| 353 |
*usr_t = r.ru_utime.tv_sec + 1e-6 * r.ru_utime.tv_usec; |
| 354 |
*sys_t = r.ru_stime.tv_sec + 1e-6 * + r.ru_stime.tv_usec; |
| 355 |
} |
| 356 |
|
| 357 |
double cputime() |
| 358 |
{ |
| 359 |
struct rusage r; |
| 360 |
getrusage(RUSAGE_SELF, &r); |
| 361 |
return r.ru_utime.tv_sec + r.ru_stime.tv_sec + 1e-6 * (r.ru_utime.tv_usec + r.ru_stime.tv_usec); |
| 362 |
} |
| 363 |
|
| 364 |
double realtime() |
| 365 |
{ |
| 366 |
struct timeval tp; |
| 367 |
struct timezone tzp; |
| 368 |
gettimeofday(&tp, &tzp); |
| 369 |
return tp.tv_sec + tp.tv_usec * 1e-6; |
| 370 |
} |
| 371 |
|
| 372 |
long peakrss(void) |
| 373 |
{ |
| 374 |
struct rusage r; |
| 375 |
getrusage(RUSAGE_SELF, &r); |
| 376 |
#ifdef __linux__ |
| 377 |
return r.ru_maxrss * 1024; |
| 378 |
#else |
| 379 |
return r.ru_maxrss; |
| 380 |
#endif |
| 381 |
} |
| 382 |
|
| 383 |
void get_cur_time(const char *prefix) |
| 384 |
{ |
| 385 |
time_t now = time(0); |
| 386 |
struct tm ts; char buf[1024]; |
| 387 |
ts = *localtime(&now); |
| 388 |
err_printf("[%s] ", prefix); |
| 389 |
strftime(buf, sizeof(buf), "%Y-%m-%d-%s", &ts); |
| 390 |
} |
| 391 |
|
| 392 |
void print_format_time(FILE *out) |
| 393 |
{ |
| 394 |
time_t rawtime; |
| 395 |
struct tm *info; |
| 396 |
char buffer[80]; |
| 397 |
|
| 398 |
time(&rawtime); |
| 399 |
info = localtime( &rawtime ); |
| 400 |
strftime(buffer,80,"%m-%d-%Y %X", info); |
| 401 |
fprintf(out, "== %s == ", buffer); |
| 402 |
} |
| 403 |
|
| 404 |
int err_func_format_printf(const char *func, const char *format, ...) |
| 405 |
{ |
| 406 |
print_format_time(stderr); |
| 407 |
fprintf(stderr, "[%s] ", func); |
| 408 |
va_list arg; |
| 409 |
int done; |
| 410 |
va_start(arg, format); |
| 411 |
done = vfprintf(stderr, format, arg); |
| 412 |
fprintf(stderr, "\n"); |
| 413 |
int saveErrno = errno; |
| 414 |
va_end(arg); |
| 415 |
if (done < 0) _err_fatal_simple("vfprintf(stderr)", strerror(saveErrno)); |
| 416 |
return done; |
| 417 |
} |