embb_mtapi_node_t.c 11.5 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <embb/mtapi/c/mtapi.h>
#include <embb/base/c/core_set.h>

#include <mtapi_status_t.h>
#include <embb_mtapi_alloc.h>
#include <embb_mtapi_job_t.h>
#include <embb_mtapi_log.h>
#include <embb_mtapi_node_t.h>
#include <embb_mtapi_action_t.h>
#include <embb_mtapi_group_t.h>
#include <embb_mtapi_task_t.h>
#include <embb_mtapi_queue_t.h>
#include <embb_mtapi_scheduler_t.h>
#include <embb_mtapi_attr.h>

42 43
#include <embb/base/c/internal/cmake_config.h>

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

static embb_mtapi_node_t* embb_mtapi_node_instance = NULL;

/* ---- CLASS MEMBERS ------------------------------------------------------ */

mtapi_boolean_t embb_mtapi_node_is_initialized() {
  return (mtapi_boolean_t)(embb_mtapi_node_instance != NULL);
}

embb_mtapi_node_t* embb_mtapi_node_get_instance() {
  return embb_mtapi_node_instance;
}


/* ---- INTERFACE FUNCTIONS ------------------------------------------------ */

void mtapi_initialize(
  MTAPI_IN mtapi_domain_t domain_id,
  MTAPI_IN mtapi_node_t node_id,
  MTAPI_IN mtapi_node_attributes_t* attributes,
  MTAPI_OUT mtapi_info_t* mtapi_info,
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
  embb_mtapi_node_t* node;

  embb_mtapi_log_trace(
    "mtapi_initialize() called (domain: %i, node: %i)\n", domain_id, node_id);

  /* check if node was already initialized */
  if (embb_mtapi_node_is_initialized()) {
    local_status = MTAPI_ERR_NODE_INITIALIZED;

    node = embb_mtapi_node_instance;

    /* return previously set information structure */
    if (MTAPI_NULL != mtapi_info) {
      *mtapi_info = node->info;
    }
  } else {
    embb_mtapi_alloc_reset_bytes_allocated();
    /* create node instance */
    embb_mtapi_node_instance = (embb_mtapi_node_t*)
      embb_mtapi_alloc_allocate(sizeof(embb_mtapi_node_t));
    if (NULL == embb_mtapi_node_instance) {
      /* out of memory! */
      local_status = MTAPI_ERR_UNKNOWN;
    } else {
      node = embb_mtapi_node_instance;

      node->domain_id = domain_id;
      node->node_id = node_id;

      if (MTAPI_NULL != attributes) {
        node->attributes = *attributes;
        local_status = MTAPI_SUCCESS;
      } else {
        mtapi_nodeattr_init(&node->attributes, &local_status);
      }

      if (MTAPI_SUCCESS == local_status) {
        mtapi_affinity_init(&node->affinity_all, MTAPI_TRUE, &local_status);
      }

      if (MTAPI_SUCCESS == local_status) {
108
        embb_atomic_init_int(&node->is_scheduler_running, MTAPI_FALSE);
109 110 111 112 113 114 115 116 117 118 119

        /* initialize storage */
        embb_mtapi_job_initialize_list(node);
        node->action_pool = embb_mtapi_action_pool_new(
          node->attributes.max_actions);
        node->group_pool = embb_mtapi_group_pool_new(
          node->attributes.max_groups);
        node->task_pool = embb_mtapi_task_pool_new(
          node->attributes.max_tasks);
        node->queue_pool = embb_mtapi_queue_pool_new(
          node->attributes.max_queues);
120 121 122 123 124 125 126 127
        if (MTAPI_NULL == node->job_list ||
          MTAPI_NULL == node->action_pool ||
          MTAPI_NULL == node->group_pool ||
          MTAPI_NULL == node->task_pool ||
          MTAPI_NULL == node->queue_pool) {
          mtapi_finalize(NULL);
          local_status = MTAPI_ERR_NODE_INITFAILED;
        }
128

Tobias Langer committed
129 130 131 132 133 134
#ifdef EMBB_HARD_REALTIME
         embb_mtapi_task_queue_initialize_with_capacity(
           &node->global_task_queue,
           node->attributes.max_tasks);
#endif

135 136 137 138 139
        if (local_status == MTAPI_SUCCESS) {
          /* initialize scheduler for local node */
          node->scheduler = embb_mtapi_scheduler_new();
          if (MTAPI_NULL != node->scheduler) {
            /* fill information structure */
140 141 142 143 144 145
            node->info.mtapi_version = 0x1000; // mtapi version 1.0
            node->info.organization_id = MCA_ORG_ID_EMB;
            node->info.implementation_version =
              EMBB_BASE_VERSION_MAJOR * 0x1000 + EMBB_BASE_VERSION_MINOR;
            node->info.number_of_domains = ~0u;
            node->info.number_of_nodes = ~0u;
146 147 148 149 150 151 152 153 154
            node->info.hardware_concurrency = embb_core_count_available();
            node->info.used_memory = embb_mtapi_alloc_get_bytes_allocated();
            if (MTAPI_NULL != mtapi_info) {
              *mtapi_info = node->info;
            }

            /* initialization succeeded, tell workers to start working */
            embb_atomic_store_int(&node->is_scheduler_running, MTAPI_TRUE);
          } else {
155 156 157
            mtapi_finalize(MTAPI_NULL);
            local_status = MTAPI_ERR_NODE_INITFAILED;
          }
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        }
      } else {
        embb_mtapi_alloc_deallocate(node);
        local_status = MTAPI_ERR_PARAMETER;
      }
    }
  }

  mtapi_status_set(status, local_status);
}

void mtapi_finalize(MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;

  embb_mtapi_log_trace("mtapi_finalize() called\n");

  if (embb_mtapi_node_is_initialized()) {
    embb_mtapi_node_t* node = embb_mtapi_node_get_instance();

    /* finalize scheduler */
    if (MTAPI_NULL != node->scheduler) {
      embb_mtapi_scheduler_delete(node->scheduler);
      node->scheduler = MTAPI_NULL;
    }

    /* finalize storage in reverse order */
184 185 186 187
    if (MTAPI_NULL != node->queue_pool) {
      embb_mtapi_queue_pool_delete(node->queue_pool);
      node->queue_pool = MTAPI_NULL;
    }
188

189 190 191 192
    if (MTAPI_NULL != node->task_pool) {
      embb_mtapi_task_pool_delete(node->task_pool);
      node->task_pool = MTAPI_NULL;
    }
193

194 195 196 197
    if (MTAPI_NULL != node->group_pool) {
      embb_mtapi_group_pool_delete(node->group_pool);
      node->group_pool = MTAPI_NULL;
    }
198

199 200 201 202
    if (MTAPI_NULL != node->action_pool) {
      embb_mtapi_action_pool_delete(node->action_pool);
      node->action_pool = MTAPI_NULL;
    }
203

204 205 206
    if (MTAPI_NULL != node->job_list) {
      embb_mtapi_job_finalize_list(node);
    }
207

208
    embb_atomic_destroy_int(&node->is_scheduler_running);
Tobias Langer committed
209 210 211
#ifdef EMBB_HARD_REALTIME
    embb_mtapi_task_queue_finalize(&node->global_task_queue);
#endif
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    /* free system instance */
    embb_mtapi_alloc_deallocate(node);
    embb_mtapi_node_instance = MTAPI_NULL;

    local_status = MTAPI_SUCCESS;
  } else {
    local_status = MTAPI_ERR_NODE_NOTINIT;
  }

  mtapi_status_set(status, local_status);
}

void mtapi_node_get_attribute(
  MTAPI_IN mtapi_node_t node,
  MTAPI_IN mtapi_uint_t attribute_num,
  MTAPI_OUT void* attribute,
  MTAPI_IN mtapi_size_t attribute_size,
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
  embb_mtapi_node_t* local_node = embb_mtapi_node_get_instance();

  embb_mtapi_log_trace("mtapi_node_get_attribute() called\n");

  if (embb_mtapi_node_is_initialized()) {
    if (local_node->node_id == node) {
      if (MTAPI_NULL != attribute) {
        switch (attribute_num) {
        case MTAPI_NODE_CORE_AFFINITY:
          if (MTAPI_NODE_CORE_AFFINITY_SIZE == attribute_size) {
            *(embb_core_set_t*)attribute =
              local_node->attributes.core_affinity;
            local_status = MTAPI_SUCCESS;
          } else {
            local_status = MTAPI_ERR_ATTR_SIZE;
          }
          break;

        case MTAPI_NODE_NUMCORES:
          local_status = embb_mtapi_attr_get_mtapi_uint_t(
            &local_node->attributes.num_cores, attribute, attribute_size);
          break;

        case MTAPI_NODE_TYPE:
          local_status = embb_mtapi_attr_get_mtapi_uint_t(
            &local_node->attributes.type, attribute, attribute_size);
          break;

        case MTAPI_NODE_MAX_TASKS:
          local_status = embb_mtapi_attr_get_mtapi_uint_t(
            &local_node->attributes.max_tasks, attribute, attribute_size);
          break;

        case MTAPI_NODE_MAX_ACTIONS:
          local_status = embb_mtapi_attr_get_mtapi_uint_t(
            &local_node->attributes.max_actions, attribute, attribute_size);
          break;

        case MTAPI_NODE_MAX_GROUPS:
          local_status = embb_mtapi_attr_get_mtapi_uint_t(
            &local_node->attributes.max_groups, attribute, attribute_size);
          break;

        case MTAPI_NODE_MAX_QUEUES:
          local_status = embb_mtapi_attr_get_mtapi_uint_t(
            &local_node->attributes.max_queues, attribute, attribute_size);
          break;

        case MTAPI_NODE_QUEUE_LIMIT:
          local_status = embb_mtapi_attr_get_mtapi_uint_t(
            &local_node->attributes.queue_limit, attribute, attribute_size);
          break;

        case MTAPI_NODE_MAX_JOBS:
          local_status = embb_mtapi_attr_get_mtapi_uint_t(
            &local_node->attributes.max_jobs, attribute, attribute_size);
          break;

        case MTAPI_NODE_MAX_ACTIONS_PER_JOB:
          local_status = embb_mtapi_attr_get_mtapi_uint_t(
            &local_node->attributes.max_actions_per_job, attribute,
            attribute_size);
          break;

        case MTAPI_NODE_MAX_PRIORITIES:
          local_status = embb_mtapi_attr_get_mtapi_uint_t(
            &local_node->attributes.max_priorities, attribute, attribute_size);
          break;

        default:
          local_status = MTAPI_ERR_ATTR_NUM;
          break;
        }
      } else {
        local_status = MTAPI_ERR_PARAMETER;
      }
    } else {
      local_status = MTAPI_ERR_NODE_NOTINIT;
    }
  } else {
    local_status = MTAPI_ERR_NODE_NOTINIT;
  }

  mtapi_status_set(status, local_status);
}

mtapi_domain_t mtapi_domain_id_get(
  MTAPI_OUT mtapi_status_t* status
  ) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
  embb_mtapi_node_t* node = embb_mtapi_node_get_instance();
  mtapi_domain_t domain_id = MTAPI_DOMAIN_ID_INVALID;

  embb_mtapi_log_trace("mtapi_domain_id_get() called\n");

  if (embb_mtapi_node_is_initialized()) {
    domain_id = node->domain_id;
    local_status = MTAPI_SUCCESS;
  } else {
    local_status = MTAPI_ERR_NODE_NOTINIT;
  }

  mtapi_status_set(status, local_status);
  return domain_id;
}

mtapi_node_t mtapi_node_id_get(
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
  embb_mtapi_node_t* node = embb_mtapi_node_get_instance();
  mtapi_node_t node_id = MTAPI_NODE_ID_INVALID;

  embb_mtapi_log_trace("mtapi_domain_id_get() called\n");

  if (embb_mtapi_node_is_initialized()) {
    node_id = node->node_id;
    local_status = MTAPI_SUCCESS;
  } else {
    local_status = MTAPI_ERR_NODE_NOTINIT;
  }

  mtapi_status_set(status, local_status);
  return node_id;
}