Пример #1
0
/**
       * Initialization function for process that
       * wants to register heartbeats
       * @param hb pointer to heartbeat_t
       * @param min_target double
       * @param max_target double
       * @param window_size int64_t
       * @param buffer_depth int64_t
       * @param log_name pointer to char
       */
int heartbeat_init(heartbeat_t* hb,
                   double min_target,
                   double max_target,
                   int64_t window_size,
                   int64_t buffer_depth,
                   char* log_name) {
    // FILE* file;
    int rc = 0;
    int pid = getpid();
    //  char hb_filename[256];

    hb->state = HB_alloc_state(pid);
    hb->state->pid = pid;

    if(log_name != NULL) {
        hb->text_file = fopen(log_name, "w");
        fprintf(hb->text_file, "Beat    Tag    Timestamp    Global Rate    Window Rate    Instant Rate\n" );
    }
    else
        hb->text_file = NULL;

    if(getenv("HEARTBEAT_ENABLED_DIR") == NULL)
        return 1;

    sprintf(hb->filename, "%s/%d", getenv("HEARTBEAT_ENABLED_DIR"), hb->state->pid);


    hb->log = HB_alloc_log(hb->state->pid, buffer_depth);

    if(hb->log == NULL)
        rc = 2;

    hb->first_timestamp = hb->last_timestamp = -1;
    hb->state->window_size = window_size;
    hb->window = (int64_t*) malloc(window_size*sizeof(int64_t));
    hb->current_index = 0;
    hb->state->min_heartrate = min_target;
    hb->state->max_heartrate = max_target;
    hb->state->counter = 0;
    hb->state->buffer_index = 0;
    hb->state->read_index = 0;
    hb->state->buffer_depth = buffer_depth;
    pthread_mutex_init(&hb->mutex, NULL);
    hb->steady_state = 0;
    hb->state->valid = 0;

    hb->binary_file = fopen(hb->filename, "w");
    if ( hb->binary_file == NULL ) {
        return 1;
    }
    fclose(hb->binary_file);


    return rc;
}
Пример #2
0
heartbeat_t* heartbeat_init(int64_t window_size,
                            int64_t buffer_depth,
                            const char* log_name,
                            double min_target,
                            double max_target) {
  int pid = getpid();

  heartbeat_t* hb = (heartbeat_t*) malloc(sizeof(heartbeat_t));
  if (hb == NULL) {
    perror("Failed to malloc heartbeat");
    return NULL;
  }
  // set to NULL so free doesn't fail in finish function if we have to abort
  hb->window = NULL;
  hb->text_file = NULL;

  hb->state = HB_alloc_state(pid);
  if (hb->state == NULL) {
    heartbeat_finish(hb);
    return NULL;
  }
  hb->state->pid = pid;

  if(log_name != NULL) {
    hb->text_file = fopen(log_name, "w");
    if (hb->text_file == NULL) {
      perror("Failed to open heartbeat log file");
      heartbeat_finish(hb);
      return NULL;
    } else {
      fprintf(hb->text_file, "Beat    Tag    Timestamp    Global Rate    Window Rate    Instant Rate\n" );
    }
  } else {
    hb->text_file = NULL;
  }

  if(getenv("HEARTBEAT_ENABLED_DIR") == NULL) {
    heartbeat_finish(hb);
    return NULL;
  }
  sprintf(hb->filename, "%s/%d", getenv("HEARTBEAT_ENABLED_DIR"), hb->state->pid);
  printf("%s\n", hb->filename);

  hb->log = HB_alloc_log(hb->state->pid, buffer_depth);
  if(hb->log == NULL) {
    heartbeat_finish(hb);
    return NULL;
  }

  hb->first_timestamp = hb->last_timestamp = -1;
  hb->state->window_size = window_size;
  hb->window = (int64_t*) malloc(window_size*sizeof(int64_t));
  if (hb->window == NULL) {
    perror("Failed to malloc window size");
    heartbeat_finish(hb);
    return NULL;
  }
  hb->current_index = 0;
  hb->state->min_heartrate = min_target;
  hb->state->max_heartrate = max_target;
  hb->state->counter = 0;
  hb->state->buffer_index = 0;
  hb->state->read_index = 0;
  hb->state->buffer_depth = buffer_depth;
  pthread_mutex_init(&hb->mutex, NULL);
  hb->steady_state = 0;
  hb->state->valid = 0;

  hb->binary_file = fopen(hb->filename, "w");
  if ( hb->binary_file == NULL ) {
    perror("Failed to open heartbeat log");
    heartbeat_finish(hb);
    return NULL;
  }
  fclose(hb->binary_file);

  return hb;
}