Browse Source

use std::chrono instead of clock_gettime

David Mazieres 6 years ago
parent
commit
3ff7ad90a3
5 changed files with 54 additions and 21 deletions
  1. 30
    1
      configure.ac
  2. 9
    13
      misc.cc
  3. 6
    0
      misc.h
  4. 7
    5
      protocol.cc
  5. 2
    2
      xapian_sync.cc

+ 30
- 1
configure.ac View File

@@ -21,12 +21,41 @@ PKG_CHECK_MODULES([libcrypto], [libcrypto])
21 21
 AC_PATH_PROG(XAPIAN_CONFIG, xapian-config)
22 22
 test -n "$XAPIAN_CONFIG" || AC_MSG_ERROR(Cannot find xapian-config)
23 23
 if ! xapian_CPPFLAGS=$($XAPIAN_CONFIG --cxxflags) \
24
-   || ! xapian_LIBS=$($XAPIAN_CONFIG --ltlibs); then
24
+   || ! xapian_LIBS=$($XAPIAN_CONFIG --libs); then
25 25
    AC_MSG_ERROR(Error running $XAPIAN_CONFIG)
26 26
 fi
27 27
 AC_SUBST(xapian_CPPFLAGS)
28 28
 AC_SUBST(xapian_LIBS)
29 29
 
30
+AC_MSG_CHECKING(For st_mtim in struct stat)
31
+AC_COMPILE_IFELSE(
32
+[AC_LANG_PROGRAM([#include <time.h>
33
+#include <sys/time.h>
34
+#include <sys/stat.h>],
35
+[int sz = sizeof(stat::st_mtim);])],
36
+have_st_mtim=yes, have_st_mtim=no)
37
+AC_MSG_RESULT($have_st_mtim)
38
+
39
+if test yes = "$have_st_mtim"; then
40
+   ST_MTIM=st_mtim
41
+else
42
+   AC_MSG_CHECKING(For st_mtimespec in struct stat)
43
+   AC_COMPILE_IFELSE(
44
+   [AC_LANG_PROGRAM([#include <time.h>
45
+#include <sys/time.h>
46
+#include <sys/stat.h>],
47
+   [int sz = sizeof(stat::st_mtimespec);])],
48
+   have_st_mtimespec=yes, have_st_mtimespec=no)
49
+   AC_MSG_RESULT($have_st_mtimespec)
50
+   if test yes = "$have_st_mtimespec"; then
51
+      ST_MTIM=st_mtimespec
52
+   else
53
+	AC_MSG_ERROR(Cannot find nanoseconds mtime in stat struct)
54
+   fi
55
+fi
56
+AC_DEFINE_UNQUOTED(ST_MTIM, $ST_MTIM,
57
+Name of timespec modification time field in stat structure)
58
+
30 59
 AC_CONFIG_FILES([Makefile])
31 60
 AC_OUTPUT
32 61
 

+ 9
- 13
misc.cc View File

@@ -1,7 +1,8 @@
1
-#include <stdexcept>
1
+#include <chrono>
2 2
 #include <iomanip>
3 3
 #include <iostream>
4 4
 #include <sstream>
5
+#include <stdexcept>
5 6
 #include <fcntl.h>
6 7
 #include <string.h>
7 8
 #include <unistd.h>
@@ -112,26 +113,21 @@ hash_ctx::final()
112 113
   return hexdump ({ reinterpret_cast<const char *> (resbuf), sizeof (resbuf) });
113 114
 }
114 115
 
115
-static double
116
-time_stamp ()
117
-{
118
-  timespec ts;
119
-  clock_gettime (CLOCK_REALTIME, &ts);
120
-  return ts_to_double (ts);
121
-}
116
+using stp = std::chrono::time_point<std::chrono::steady_clock>;
122 117
 
123
-static double start_time_stamp {time_stamp()};
124
-static double last_time_stamp {start_time_stamp};
118
+stp start_time_stamp{stp::clock::now()};
119
+stp last_time_stamp{start_time_stamp};
125 120
 
126 121
 void
127 122
 print_time (string msg)
128 123
 {
129
-  double now = time_stamp();
124
+  using namespace std::chrono;
125
+  stp now = stp::clock::now();
130 126
   if (opt_verbose > 0) {
131 127
     auto oldFlags = cerr.flags();
132 128
     cerr.setf (ios::fixed, ios::floatfield);
133
-    cerr << msg << "... " << now - start_time_stamp
134
-	 << " (+" << now - last_time_stamp << ")\n";
129
+    cerr << msg << "... " << duration<double>(now - start_time_stamp).count()
130
+	 << " (+" << duration<double>(now - last_time_stamp).count() << ")\n";
135 131
     cerr.flags (oldFlags);
136 132
   }
137 133
   last_time_stamp = now;

+ 6
- 0
misc.h View File

@@ -5,8 +5,14 @@
5 5
 
6 6
 #include <cstddef>
7 7
 #include <string>
8
+#include <time.h>
9
+#include <sys/time.h>
8 10
 #include <openssl/sha.h>
9 11
 
12
+#ifndef ST_MTIM
13
+#define ST_MTIM 1
14
+#endif //!ST_MTIM
15
+
10 16
 using std::string;
11 17
 
12 18
 extern int opt_verbose;

+ 7
- 5
protocol.cc View File

@@ -11,7 +11,6 @@
11 11
 #include <vector>
12 12
 #include <fcntl.h>
13 13
 #include <signal.h>
14
-#include <time.h>
15 14
 #include <unistd.h>
16 15
 #include <sys/stat.h>
17 16
 #include <sys/types.h>
@@ -101,9 +100,12 @@ maildir_name()
101 100
   static int ndeliveries = 0;
102 101
 
103 102
   ostringstream os;
104
-  struct timespec ts;
105
-  clock_gettime (CLOCK_REALTIME, &ts);
106
-  os << ts.tv_sec << ".M" << ts.tv_nsec << 'P' << pid
103
+  using namespace std::chrono;
104
+  auto now = system_clock::now().time_since_epoch();
105
+  
106
+  os << duration_cast<seconds>(now).count()
107
+     << ".M" << duration_cast<nanoseconds>(now % seconds(1)).count()
108
+     << 'P' << pid
107 109
      << 'Q' << ++ndeliveries
108 110
      << 'R' << setfill('0') << hex << setw(2 * sizeof(randint())) << randint()
109 111
      << '.' << hostname;
@@ -393,7 +395,7 @@ msg_sync::hash_sync(const versvector &rvv,
393 395
 					      tip ? &tip->tags : nullptr,
394 396
 					      &isnew));
395 397
       i64 dir_id = get_dir_docid(li.first);
396
-      add_file_.reset().param(dir_id, newname, docid, ts_to_double(sb.st_mtim),
398
+      add_file_.reset().param(dir_id, newname, docid, ts_to_double(sb.ST_MTIM),
397 399
 			      i64(sb.st_ino), hashdb.hash_id()).step();
398 400
       if (isnew) {
399 401
 	record_docid_.reset().param(rhi.message_id, docid).step();

+ 2
- 2
xapian_sync.cc View File

@@ -405,7 +405,7 @@ fileops::add_file(const string &dir, int dfd, i64 dir_docid,
405 405
 
406 406
   i64 hash_id = get_file_hash_id(dfd, name, docid);
407 407
   add_file_.reset()
408
-    .param(dir_docid, name, docid, ts_to_double(sb.st_mtim),
408
+    .param(dir_docid, name, docid, ts_to_double(sb.ST_MTIM),
409 409
 	   i64(sb.st_ino), hash_id).step();
410 410
 }
411 411
 
@@ -424,7 +424,7 @@ fileops::check_file(const string &dir, int dfd, i64 dir_docid)
424 424
   if (!S_ISREG(sb.st_mode))
425 425
     return;
426 426
 
427
-  double fs_mtim = ts_to_double(sb.st_mtim);
427
+  double fs_mtim = ts_to_double(sb.ST_MTIM);
428 428
   i64 fs_inode = sb.st_ino, fs_size = sb.st_size;
429 429
   double db_mtim = scan_dir_.real(3);
430 430
   i64 db_inode = scan_dir_.integer(4);

Loading…
Cancel
Save