X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fceph%2Fsrc%2Fcommon%2Fceph_time.cc;fp=src%2Fceph%2Fsrc%2Fcommon%2Fceph_time.cc;h=be7c5f889c6a3cf3412e62d5408c728f3306b5fc;hb=812ff6ca9fcd3e629e49d4328905f33eee8ca3f5;hp=0000000000000000000000000000000000000000;hpb=15280273faafb77777eab341909a3f495cf248d9;p=stor4nfv.git diff --git a/src/ceph/src/common/ceph_time.cc b/src/ceph/src/common/ceph_time.cc new file mode 100644 index 0000000..be7c5f8 --- /dev/null +++ b/src/ceph/src/common/ceph_time.cc @@ -0,0 +1,129 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2004-2006 Sage Weil + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +// For ceph_timespec +#include "ceph_time.h" +#include "config.h" + +#if defined(DARWIN) +int clock_gettime(int clk_id, struct timespec *tp) +{ + if (clk_id == CALENDAR_CLOCK) { + // gettimeofday is much faster than clock_get_time + struct timeval now; + int ret = gettimeofday(&now, NULL); + if (ret) + return ret; + tp->tv_sec = now.tv_sec; + tp->tv_nsec = now.tv_usec * 1000L; + } else { + clock_serv_t cclock; + mach_timespec_t mts; + + host_get_clock_service(mach_host_self(), clk_id, &cclock); + clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + + tp->tv_sec = mts.tv_sec; + tp->tv_nsec = mts.tv_nsec; + } + return 0; +} +#endif + +namespace ceph { + namespace time_detail { + void real_clock::to_ceph_timespec(const time_point& t, + struct ceph_timespec& ts) { + ts.tv_sec = to_time_t(t); + ts.tv_nsec = (t.time_since_epoch() % seconds(1)).count(); + } + struct ceph_timespec real_clock::to_ceph_timespec(const time_point& t) { + struct ceph_timespec ts; + to_ceph_timespec(t, ts); + return ts; + } + real_clock::time_point real_clock::from_ceph_timespec( + const struct ceph_timespec& ts) { + return time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec)); + } + + void coarse_real_clock::to_ceph_timespec(const time_point& t, + struct ceph_timespec& ts) { + ts.tv_sec = to_time_t(t); + ts.tv_nsec = (t.time_since_epoch() % seconds(1)).count(); + } + struct ceph_timespec coarse_real_clock::to_ceph_timespec( + const time_point& t) { + struct ceph_timespec ts; + to_ceph_timespec(t, ts); + return ts; + } + coarse_real_clock::time_point coarse_real_clock::from_ceph_timespec( + const struct ceph_timespec& ts) { + return time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec)); + } + } + + using std::chrono::duration_cast; + using std::chrono::seconds; + using std::chrono::microseconds; + + template::type*> + std::ostream& operator<<(std::ostream& m, + const std::chrono::time_point& t) { + return m << std::chrono::duration(t.time_since_epoch()).count() + << "s"; + } + + std::ostream& operator<<(std::ostream& m, const timespan& t) { + return m << std::chrono::duration(t).count() << "s"; + } + + template::type*> + std::ostream& operator<<(std::ostream& m, + const std::chrono::time_point& t) { + m.setf(std::ios::right); + char oldfill = m.fill(); + m.fill('0'); + // localtime. this looks like an absolute time. + // aim for http://en.wikipedia.org/wiki/ISO_8601 + struct tm bdt; + time_t tt = Clock::to_time_t(t); + localtime_r(&tt, &bdt); + m << std::setw(4) << (bdt.tm_year+1900) // 2007 -> '07' + << '-' << std::setw(2) << (bdt.tm_mon+1) + << '-' << std::setw(2) << bdt.tm_mday + << ' ' + << std::setw(2) << bdt.tm_hour + << ':' << std::setw(2) << bdt.tm_min + << ':' << std::setw(2) << bdt.tm_sec + << "." << std::setw(6) << duration_cast( + t.time_since_epoch() % seconds(1)); + m.fill(oldfill); + m.unsetf(std::ios::right); + return m; + } + + template std::ostream& + operator<< (std::ostream& m, const mono_time& t); + template std::ostream& + operator<< (std::ostream& m, const real_time& t); + template std::ostream& + operator<< (std::ostream& m, const coarse_mono_time& t); + template std::ostream& + operator<< (std::ostream& m, const coarse_real_time& t); +}