Paste number 154647: werker

Paste number 154647: werker
Pasted by: phf
When:9 years, 6 months ago
Share:Tweet this! | http://paste.lisp.org/+3BBR
Channel:None
Paste contents:
Raw Source | XML | Display As
--- werker.c~1 ce96de2a104e17c3c381b70cb372928fd7fe473d827da103a003900d75569118e0569a54fdc8edab1695d3b6344a79244384d3a76fe0f23a59656faed654f9a1
+++ werker.c d8f3457cd07aec9b6b1457995dad6221af3eaad01971defa0699cfa01f87bb3016b089a8bea3998a5c04efd14ccd15fd5ebd3dfb0555f94211ec05b450864d3a
@@ -6,6 +6,8 @@
 #include <error.h>
 #include <sqlite3.h>
 #include <gmp.h>
+#include <sys/time.h>
+#include <signal.h>
 
 
 char *prodpath;
@@ -14,7 +16,9 @@
 int db_poll_interval;
 MP_INT prod;
 int upd_enable;
-
+int do_snapshot=0;
+#define SNAPSHOT_NAME "db_snapshot.%ld"
+#define SNAPSHOT {if(do_snapshot) tod_snapshot_db();}
 
 sqlite3 *open_db(char *dbpath) {
   sqlite3 *db;
@@ -27,6 +31,71 @@
   return db;
 }
 
+/* http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html */
+int
+timeval_subtract (result, x, y)
+     struct timeval *result, *x, *y;
+{
+  /* Perform the carry for the later subtraction by updating y. */
+  if (x->tv_usec < y->tv_usec) {
+    int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
+    y->tv_usec -= 1000000 * nsec;
+    y->tv_sec += nsec;
+  }
+  if (x->tv_usec - y->tv_usec > 1000000) {
+    int nsec = (x->tv_usec - y->tv_usec) / 1000000;
+    y->tv_usec += 1000000 * nsec;
+    y->tv_sec -= nsec;
+  }
+
+  /* Compute the time remaining to wait.
+     tv_usec is certainly positive. */
+  result->tv_sec = x->tv_sec - y->tv_sec;
+  result->tv_usec = x->tv_usec - y->tv_usec;
+
+  /* Return 1 if result is negative. */
+  return x->tv_sec < y->tv_sec;
+}
+
+void snapshot_db(char *path) {
+  int rc;
+  sqlite3 *backdb;
+  sqlite3_backup *backup;
+  struct timeval tp, tp2, tpdiff;
+  printf("backup to %s\n", path);
+  gettimeofday(&tp, NULL);
+  rc = sqlite3_open(path, backdb);
+  if(rc != SQLITE_OK) goto done;
+  backup = sqlite3_backup_init(backdb, "main", db, "main");
+  if(backup) {
+    sqlite3_backup_step(backup, -1);
+    sqlite3_backup_finish(backup);
+  }
+  rc = sqlite3_errcode(backdb);
+ done:
+  do_snapshot=0;
+  if(rc != SQLITE_OK) {
+    printf("error during backup %s\n", sqlite3_errmsg(db));
+  }
+  sqlite3_close(backdb);
+  getimeofday(&tp2);
+  timeval_subtract(&tpdiff, &tp, &tp2);
+  printf("runtime %ld.%06ld\n", tpdiff.ru_stime.tv_sec,
+         tpdiff.ru_stime.tv_usec);
+}
+
+void tod_snapshot_db() {
+  char buf[1024];
+  struct timeval tp;
+  gettimeofday(&tp; NULL);
+  sprintf(buf, SNAPSHOT_NAME, tp.ru_stime.tv_sec);
+  snapshot_db(buf);
+}
+
+void cb_sighup(sig_t sig) {
+  printf("requesting backup\n");
+  do_snapshot=1;
+}
 
 void db_upd_mod(char *mod, char *new_p, int ticket) {
   int rc;
@@ -109,6 +178,7 @@
       do_gcd_mod(argv[i]);
     }
   }
+  SNAPSHOT;
   return 0;
 }
 
@@ -155,6 +225,7 @@
 
   dbpath = argv[2];
   prodpath = argv[3];
+  signal(SIGHUP, cb_sighup);
   mpz_init(&prod);
 
   gen_prod();
@@ -163,6 +234,7 @@
     sleep(db_poll_interval);
     db = open_db(dbpath);
     db_walk_mods("select mod from moduli where ticket = 1", cb_mods);
+    SNAPSHOT;
     sqlite3_close(db);
     char *prod_s = mpz_get_str(NULL, 10, &prod);
     save_file(prod_s, strlen(prod_s), prodpath);

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.