Beispiel #1
0
static void
s_once_proc(void) {
  lagopus_result_t r;

  if ((r = lagopus_mutex_create(&s_sched_lck)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the callout scheduler main mutex.\n");
  }

  if ((r = lagopus_mutex_create(&s_lck)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the callout global mutex.\n");
  }

  if ((r = lagopus_cond_create(&s_sched_cnd)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the callout cond.\n");
  }

  if ((r = lagopus_hashmap_create(&s_tsk_tbl,
                                  LAGOPUS_HASHMAP_TYPE_ONE_WORD,
                                  NULL)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the callout table.\n");
  }

  if ((r = lagopus_bbq_create(&s_urgent_tsk_q, lagopus_callout_task_t,
                              CALLOUT_TASK_MAX, s_task_freeup)) != 
      LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the callout urgent tasks queue.\n");
  }

  if ((r = lagopus_bbq_create(&s_idle_tsk_q, lagopus_callout_task_t,
                              CALLOUT_TASK_MAX, s_task_freeup)) !=
      LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the callout idle tasks queue.\n");
  }

  if ((r = lagopus_mutex_create_recursive(&s_q_lck)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    lagopus_exit_fatal("can't initialize the callout task queue mutex.\n");
  }

  TAILQ_INIT(&s_chrono_tsk_q);
}
Beispiel #2
0
static int
do_run(size_t nthds, ssize_t nputs) {
  int ret = 1;

  lagopus_result_t r;

  size_t i;
  size_t j;
  char thdname[16];

  lagopus_chrono_t *put_dates = NULL;

  size_t n_need_watch = 0;
  size_t n_valid_polls = 0;
  ssize_t qsz;

  test_thread_t tt;
  lagopus_bbq_t bbq;

  lagopus_chrono_t t_begin;
  lagopus_chrono_t t_end;

  lagopus_chrono_t p_begin;
  lagopus_chrono_t p_t;

  ssize_t n_gets = 0;
  lagopus_chrono_t p_min = LLONG_MAX;
  lagopus_chrono_t p_max = LLONG_MIN;
  double p_sum = 0.0;
  double p_sum2 = 0.0;

  double p_avg;
  double p_sd;

  lagopus_chrono_t t_total = 0;
  double t_avg;

  /*
   * This is only for choking clang/scan-build.
   */
  WHAT_TIME_IS_IT_NOW_IN_NSEC(t_begin);

  put_dates = (lagopus_chrono_t *)
              malloc(sizeof(lagopus_chrono_t) * (size_t)nputs);
  if (put_dates == NULL) {
    goto done;
  }

  if ((r = lagopus_mutex_create(&start_lock)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    goto done;
  }
  if ((r = lagopus_mutex_create(&stop_lock)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    goto done;
  }

  /*
   * Create the qmuxer.
   */
  if ((r = lagopus_qmuxer_create(&qmx)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(r);
    goto done;
  }

  /*
   * Then create queues.
   */
  bbqs = (lagopus_bbq_t *)malloc(sizeof(lagopus_bbq_t) * nthds);
  if (bbqs == NULL) {
    goto done;
  }
  for (i = 0; i < nthds; i++) {
    if ((r = lagopus_bbq_create(&(bbqs[i]), lagopus_chrono_t,
                                1000LL * 1000LL,
                                NULL)) != LAGOPUS_RESULT_OK) {
      lagopus_perror(r);
      goto done;
    }
    n_created_bbqs++;
  }
  if (n_created_bbqs == 0) {
    goto done;
  }

  /*
   * Then create poll objects for the each queue.
   */
  polls = (lagopus_qmuxer_poll_t *)malloc(sizeof(lagopus_qmuxer_poll_t) *
                                          n_created_bbqs);
  if (polls == NULL) {
    goto done;
  }
  for (i = 0; i < n_created_bbqs; i++) {
    if ((r = lagopus_qmuxer_poll_create(&(polls[i]),
                                        bbqs[i],
                                        LAGOPUS_QMUXER_POLL_READABLE)) !=
        LAGOPUS_RESULT_OK) {
      lagopus_perror(r);
      goto done;
    }
    n_created_polls++;
  }
  if (n_created_polls == 0) {
    goto done;
  }

  /*
   * Then create threads for the each poll objects/queues.
   */
  tts = (test_thread_record *)malloc(sizeof(test_thread_record) *
                                     n_created_polls);
  if (tts == NULL) {
    goto done;
  }
  for (i = 0; i < n_created_polls; i++) {
    snprintf(thdname, sizeof(thdname), "putter " PFSZS(4, u), i);
    tt = &(tts[i]);
    if (test_thread_create(&tt, start_lock, bbqs[i], nputs,
                           (const char *)thdname) != true) {
      goto done;
    }
    n_created_thds++;
  }
  if (n_created_thds == 0) {
    goto done;
  }

  /*
   * Let the initiation begin.
   */

  /*
   * Ready, note that all the created threads do this lock.
   */
  (void)lagopus_mutex_lock(&start_lock);

  /*
   * Steady,
   */
  for (i = 0; i < n_created_thds; i++) {
    tt = &(tts[i]);
    if (lagopus_thread_start((lagopus_thread_t *)&tt, false) !=
        LAGOPUS_RESULT_OK) {
      (void)lagopus_mutex_unlock(&start_lock);
      goto done;
    }
  }

  fprintf(stdout, "Test for " PFSZ(u) " threads " PFSZ(u)
          " events/thdread start.\n", n_created_thds, (size_t)nputs);

  /*
   * Go.
   */
  (void)lagopus_mutex_unlock(&start_lock);

  WHAT_TIME_IS_IT_NOW_IN_NSEC(t_begin);

  while (true) {
    /*
     * Like the select(2)/poll(2), initialize poll objects before
     * checking events.
     */
    n_need_watch = 0;
    n_valid_polls = 0;
    for (i = 0; i < n_created_thds; i++) {
      /*
       * Check if the poll has a valid queue.
       */
      bbq = NULL;
      if ((r = lagopus_qmuxer_poll_get_queue(&(polls[i]), &bbq)) !=
          LAGOPUS_RESULT_OK) {
        lagopus_perror(r);
        break;
      }
      if (bbq != NULL) {
        n_valid_polls++;
      }

      /*
       * Reset the poll status.
       */
      if ((r = lagopus_qmuxer_poll_reset(&(polls[i]))) != LAGOPUS_RESULT_OK) {
        lagopus_perror(r);
        break;
      }
      n_need_watch++;
    }

    /*
     * If there are no valid queues, exit.
     */
    if (n_valid_polls == 0) {
      break;
    }

    /*
     * Wait for an event.
     *
     *  Note that we better set timeout, not waiting forever.
     */
    r = lagopus_qmuxer_poll(&qmx, (lagopus_qmuxer_poll_t *const)polls,
                            n_need_watch,
                            100LL * 1000LL * 1000LL);

    if (r > 0) {
      /*
       * Check which poll got an event. Actually, check all the queues
       * in this sample.
       */
      size_t n_actual_get = 0LL;

      for (i = 0; i < n_created_thds; i++) {

        if ((qsz = lagopus_bbq_size(&(bbqs[i]))) > 0) {

          lagopus_msg_debug(1, "Got " PFSZS(8, u) " events from the Q"
                            PFSZS(03, u) ".\n",
                            (size_t)qsz, (size_t)i);
          if ((r = lagopus_bbq_get_n(&(bbqs[i]), (void **)put_dates,
                                     (size_t)nputs, 1LL,
                                     lagopus_chrono_t,
                                     1000LL * 1000LL * 1000LL,
                                     &n_actual_get)) > 0) {
#if 1
            WHAT_TIME_IS_IT_NOW_IN_NSEC(p_begin);
#endif
            for (j = 0; j < n_actual_get; j++) {
              /*
               * In this sample, -1LL is kinda 'EOF'. Check if we got
               * the EOF.
               */
              if (put_dates[j] == -1LL) {
                /*
                 * The queue is kinda 'closed'. From now on we don't
                 * check this queue anymore. To specify this:
                 */
                lagopus_msg_debug(1, "Got an EOF from the Q" PFSZS(04, u)
                                  ".\n", i);
                goto nullify;
              }

#if 0
              WHAT_TIME_IS_IT_NOW_IN_NSEC(p_begin);
#endif

              p_t = p_begin - put_dates[j];

              if (p_t < p_min) {
                p_min = p_t;
              }
              if (p_t > p_max) {
                p_max = p_t;
              }
              p_sum += (double)p_t;
              p_sum2 += ((double)p_t * (double)p_t);
              n_gets++;
            }

          } else {
            /*
             * Something wrong for the queue. But we must not exit
             * here. Keep on checking other queues. In order to do
             * this, set NULL as the queue into the poll object for
             * the queue.
             */
            lagopus_perror(r);
          nullify:
            if ((r = lagopus_qmuxer_poll_set_queue(&(polls[i]), NULL)) ==
                LAGOPUS_RESULT_OK) {
              lagopus_msg_debug(1, "Q" PFSZS(04, u) " is not valid "
                                "anymore, ignore the queue.\n", i);
              break;
            } else {
              /*
               * There is nothing we can do now.
               */
              lagopus_perror(r);
              goto done;
            }
          }

        }

      }

    } else if (r == LAGOPUS_RESULT_TIMEDOUT) {
      lagopus_msg_debug(1, "Timedout. continue.\n");
      continue;
    } else {
      lagopus_perror(r);
      lagopus_msg_debug(1, "Break the loop due to error(s).\n");
      goto done;
    }
  }

  ret = 0;

done:

  WHAT_TIME_IS_IT_NOW_IN_NSEC(t_end);

  if (is_signaled == false) {
    fprintf(stdout, "Done.\n");
  } else {
    fprintf(stdout, "Stopped.\n");
  }

  fprintf(stdout, "Total # of the events:\t" PFSZS(22, u) "\n\n", n_gets);

  p_avg = p_sum / (double)n_gets;
  p_sd = (p_sum2 -
          2.0 * p_avg * p_sum +
          p_avg * p_avg * (double)n_gets) / (double)(n_gets - 1);
  p_sd = sqrt(p_sd);

  fprintf(stdout, "Queue stats:\n");
  fprintf(stdout, "wait time min =\t" PFSZS(22, d) " nsec.\n", p_min);
  fprintf(stdout, "wait time max =\t" PFSZS(22, d) " nsec.\n", p_max);
  fprintf(stdout, "wait time avg =\t%25.2f nsec.\n", p_avg);
  fprintf(stdout, "wait time sd =\t%25.2f.\n\n", p_sd);

  t_total = t_end - t_begin;
  t_avg = (double)t_total / (double)n_gets;

  fprintf(stdout, "Throughput:\n");
  fprintf(stdout, "total time:\t" PFSZS(22, d) " msec.\n",
          (size_t)(t_total / 1000LL / 1000LL));
  fprintf(stdout, "total avg:\t%25.2f nsec/event.\n", t_avg);

  s_destroy_all();

  free((void *)put_dates);

  return ret;
}