ATTACH_DB: simplify the code slightly and change the semantics to only
[sahlberg/ctdb.git] / common / ctdb_util.c
index 4244a046e6131a12d4bb5dbfb437a0dd5db4cfe5..1ff4c1f9aa2052cb97b46c54af6c579ab9ed430f 100644 (file)
@@ -99,14 +99,7 @@ bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
 */
 uint32_t ctdb_hash(const TDB_DATA *key)
 {
-       uint32_t value; /* Used to compute the hash value.  */
-       uint32_t i;     /* Used to cycle through random values. */
-
-       /* Set the initial value from the key size. */
-       for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
-               value = (value + (key->dptr[i] << (i*5 % 24)));
-
-       return (1103515243 * value + 12345);  
+       return tdb_jenkins_hash(discard_const(key));
 }
 
 /*
@@ -123,45 +116,11 @@ static void *_idr_find_type(struct idr_context *idp, int id, const char *type, c
        return p;
 }
 
-
-/*
-  update a max latency number
- */
-void ctdb_latency(struct ctdb_db_context *ctdb_db, const char *name, double *latency, struct timeval t)
-{
-       double l = timeval_elapsed(&t);
-       if (l > *latency) {
-               *latency = l;
-       }
-
-       if (ctdb_db->ctdb->tunable.log_latency_ms !=0) {
-               if (l*1000 > ctdb_db->ctdb->tunable.log_latency_ms) {
-                       DEBUG(DEBUG_WARNING, ("High latency %.6fs for operation %s on database %s\n", l, name, ctdb_db->db_name));
-               }
-       }
-}
-
-/*
-  update a reclock latency number
- */
-void ctdb_reclock_latency(struct ctdb_context *ctdb, const char *name, double *latency, double l)
-{
-       if (l > *latency) {
-               *latency = l;
-       }
-
-       if (ctdb->tunable.reclock_latency_ms !=0) {
-               if (l*1000 > ctdb->tunable.reclock_latency_ms) {
-                       DEBUG(DEBUG_ERR, ("High RECLOCK latency %fs for operation %s\n", l, name));
-               }
-       }
-}
-
 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state)
 {
        int id = idr_get_new_above(ctdb->idr, state, ctdb->lastid+1, INT_MAX);
        if (id < 0) {
-               DEBUG(DEBUG_NOTICE, ("Reqid wrap!\n"));
+               DEBUG(DEBUG_DEBUG, ("Reqid wrap!\n"));
                id = idr_get_new(ctdb->idr, state, INT_MAX);
        }
        ctdb->lastid = id;
@@ -326,31 +285,69 @@ struct ctdb_rec_data *ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, st
        return r;
 }
 
+
+#if HAVE_SCHED_H
+#include <sched.h>
+#endif
+
 /*
-  if possible, make this task very high priority
+  if possible, make this task real time
  */
-void ctdb_high_priority(struct ctdb_context *ctdb)
+void ctdb_set_scheduler(struct ctdb_context *ctdb)
 {
-       errno = 0;
-       if (nice(-20) == -1 && errno != 0) {
-               DEBUG(DEBUG_WARNING,("Unable to renice self: %s\n",
-                                    strerror(errno)));
+#if HAVE_SCHED_SETSCHEDULER    
+       struct sched_param p;
+       if (ctdb->saved_scheduler_param == NULL) {
+               ctdb->saved_scheduler_param = talloc_size(ctdb, sizeof(p));
+       }
+       
+       if (sched_getparam(0, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
+               DEBUG(DEBUG_ERR,("Unable to get old scheduler params\n"));
+               return;
+       }
+
+       p = *(struct sched_param *)ctdb->saved_scheduler_param;
+       p.sched_priority = 1;
+
+       if (sched_setscheduler(0, SCHED_FIFO, &p) == -1) {
+               DEBUG(DEBUG_CRIT,("Unable to set scheduler to SCHED_FIFO (%s)\n", 
+                        strerror(errno)));
        } else {
-               DEBUG(DEBUG_NOTICE,("Scheduler says I'm nice: %i\n",
-                                   getpriority(PRIO_PROCESS, getpid())));
+               DEBUG(DEBUG_NOTICE,("Set scheduler to SCHED_FIFO\n"));
        }
+#endif
 }
 
 /*
-  make ourselves slightly nicer: eg. a ctdb child.
+  restore previous scheduler parameters
  */
-void ctdb_reduce_priority(struct ctdb_context *ctdb)
+void ctdb_restore_scheduler(struct ctdb_context *ctdb)
 {
-       errno = 0;
-       if (nice(10) == -1 && errno != 0) {
-               DEBUG(DEBUG_WARNING,("Unable to lower priority: %s\n",
-                                    strerror(errno)));
+#if HAVE_SCHED_SETSCHEDULER    
+       if (ctdb->saved_scheduler_param == NULL) {
+               ctdb_fatal(ctdb, "No saved scheduler parameters\n");
+       }
+       if (sched_setscheduler(0, SCHED_OTHER, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
+               ctdb_fatal(ctdb, "Unable to restore old scheduler parameters\n");
+       }
+#endif
+}
+
+/*
+ * This function forks a child process and drops the realtime 
+ * scheduler for the child process.
+ */
+pid_t ctdb_fork(struct ctdb_context *ctdb)
+{
+       pid_t pid;
+
+       pid = fork();
+       if (pid == 0) {
+               if (ctdb->do_setsched) {
+                       ctdb_restore_scheduler(ctdb);
+               }
        }
+       return pid;
 }
 
 void set_nonblocking(int fd)
@@ -685,6 +682,6 @@ const char *ctdb_eventscript_call_names[] = {
        "status",
        "shutdown",
        "reload",
-       "ipreallocated",
-       "updateip"
+       "updateip",
+       "ipreallocated"
 };