/* * API * * loads the project's configuration from the INI file specified by `prefix`. * the INI file is `prefix`.ini. * * if some required parameters is missing, or if its value is illegal, a message * is printed and the functions returns INI_STATUS_ERROR. otherwise, the function * returns INI_STATUS_OK. * * the config argument will be populated with the values of the parameters */ int config_load(config_t * config, const char * prefix) { int res = INI_STATUS_ERROR; const char * str_value; char inifilename[MAX_INPUT_BUFFER]; inifile_t ini; config->seed_table = NULL; strncpy(inifilename, prefix, sizeof(inifilename) - 5); strcat(inifilename, ".ini"); if (ini_load(&ini, inifilename) != INI_STATUS_OK) { /* error message printed by ini_load */ return INI_STATUS_ERROR; } strncpy(config->prefix, prefix, sizeof(config->prefix)); INI_GET_STR("rule", config->rule_pattern); INI_GET_STR("lexicon_name", config->lexicon_file); INI_GET_STR("hash_name", config->hash_name); INI_GET_STR("main_rand_seed", config->random_seed); INI_GET_NUM("num_of_R", config->chain_length); INI_GET_NUM("multi_query", config->num_of_query_results); INI_GET_NUM("bucket_size", config->bucket_size); INI_GET_NUM("hash_size", config->num_of_buckets); if (strcasecmp(config->hash_name, "MD5") == 0) { config->hash_func = MD5BasicHash; config->digest_size = MD5_OUTPUT_LENGTH_IN_BYTES; } else if (strcasecmp(config->hash_name, "SHA1") == 0) { config->hash_func = SHA1BasicHash; config->digest_size = SHA1_OUTPUT_LENGTH_IN_BYTES; } else { fprintf(stderr, "INI: hash_name must be 'MD5' or 'SHA1'"); goto cleanup; } res = INI_STATUS_OK; cleanup: ini_finalize(&ini); return res; }
s32 populate_inst(ocrParamList_t **inst_param, ocrMappable_t **instance, s32 *type_counts, char ***factory_names, void ***all_factories, ocrMappable_t ***all_instances, type_enum index, dictionary *dict, char *secname) { s32 i, low, high, j; char *inststr; char key[MAX_KEY_SZ]; void *factory; s32 value; read_range(dict, secname, "id", &low, &high); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "type"); INI_GET_STR (key, inststr, ""); for (i = 0; i < type_counts[index]; i++) { if (factory_names[index][i] && (0 == strncmp(factory_names[index][i], inststr, strlen(factory_names[index][i])))) break; } if (factory_names[index][i] == NULL || strncmp(factory_names[index][i], inststr, strlen(factory_names[index][i]))) { DPRINTF(DEBUG_LVL_WARN, "Unknown type %s\n", inststr); return 0; } // find factory based on i factory = all_factories[index][i]; // Use the factory's instantiate() to create instance switch (index) { case guid_type: for (j = low; j<=high; j++) { ALLOC_PARAM_LIST(inst_param[j], paramListGuidProviderInst_t); instance[j] = (ocrMappable_t *)((ocrGuidProviderFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created guid provider of type %s, index %d\n", inststr, j); } break; case memplatform_type: for (j = low; j<=high; j++) { ALLOC_PARAM_LIST(inst_param[j], paramListMemPlatformInst_t); instance[j] = (ocrMappable_t *)((ocrMemPlatformFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created memplatform of type %s, index %d\n", inststr, j); } break; case memtarget_type: for (j = low; j<=high; j++) { ALLOC_PARAM_LIST(inst_param[j], paramListMemTargetInst_t); instance[j] = (ocrMappable_t *)((ocrMemTargetFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created memtarget of type %s, index %d\n", inststr, j); } break; case allocator_type: for (j = low; j<=high; j++) { ALLOC_PARAM_LIST(inst_param[j], paramListAllocatorInst_t); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "size"); INI_GET_INT (key, value, -1); ((paramListAllocatorInst_t *)inst_param[j])->size = value; instance[j] = (ocrMappable_t *)((ocrAllocatorFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created allocator of type %s, index %d\n", inststr, j); } break; case compplatform_type: for (j = low; j<=high; j++) { compPlatformType_t mytype = -1; TO_ENUM (mytype, inststr, compPlatformType_t, compplatform_types, compPlatformMax_id); switch (mytype) { case compPlatformPthread_id: { ALLOC_PARAM_LIST(inst_param[j], paramListCompPlatformPthread_t); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "ismasterthread"); INI_GET_BOOL (key, value, -1); ((paramListCompPlatformPthread_t *)inst_param[j])->isMasterThread = value; snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "stacksize"); INI_GET_INT (key, value, -1); ((paramListCompPlatformPthread_t *)inst_param[j])->stackSize = (value==-1)?0:value; if (key_exists(dict, secname, "binding")) { value = get_key_value(dict, secname, "binding", j-low); // printf("Binding value for %s;%d is %d\n", secname, j-low, value); } // else printf("No binding for %s\n", secname); } break; default: ALLOC_PARAM_LIST(inst_param[j], paramListCompPlatformInst_t); break; } instance[j] = (ocrMappable_t *)((ocrCompPlatformFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created compplatform of type %s, index %d\n", inststr, j); } break; case comptarget_type: for (j = low; j<=high; j++) { ALLOC_PARAM_LIST(inst_param[j], paramListCompTargetInst_t); instance[j] = (ocrMappable_t *)((ocrCompTargetFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created comptarget of type %s, index %d\n", inststr, j); } break; case workpile_type: for (j = low; j<=high; j++) { ALLOC_PARAM_LIST(inst_param[j], paramListWorkpileInst_t); instance[j] = (ocrMappable_t *)((ocrWorkpileFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created workpile of type %s, index %d\n", inststr, j); } break; case worker_type: for (j = low; j<=high; j++) { workerType_t mytype = -1; TO_ENUM (mytype, inststr, workerType_t, worker_types, workerMax_id); switch (mytype) { case workerHc_id: { ALLOC_PARAM_LIST(inst_param[j], paramListWorkerHcInst_t); ((paramListWorkerHcInst_t *)inst_param[j])->workerId = j; // using "id" for now; TODO: decide if a separate key is needed } break; default: ALLOC_PARAM_LIST(inst_param[j], paramListWorkerInst_t); break; } instance[j] = (ocrMappable_t *)((ocrWorkerFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created worker of type %s, index %d\n", inststr, j); } break; case scheduler_type: for (j = low; j<=high; j++) { schedulerType_t mytype = -1; TO_ENUM (mytype, inststr, schedulerType_t, scheduler_types, schedulerMax_id); switch (mytype) { case schedulerHc_id: { ALLOC_PARAM_LIST(inst_param[j], paramListSchedulerHcInst_t); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "workeridfirst"); INI_GET_INT (key, value, -1); ((paramListSchedulerHcInst_t *)inst_param[j])->workerIdFirst = value; } break; default: ALLOC_PARAM_LIST(inst_param[j], paramListSchedulerInst_t); break; } instance[j] = (ocrMappable_t *)((ocrSchedulerFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created scheduler of type %s, index %d\n", inststr, j); } break; case policydomain_type: for (j = low; j<=high; j++) { ocrTaskFactory_t *tf; ocrTaskTemplateFactory_t *ttf; ocrDataBlockFactory_t *dbf; ocrEventFactory_t *ef; ocrPolicyCtxFactory_t *cf; ocrGuidProvider_t *gf; ocrLockFactory_t *lf; ocrAtomic64Factory_t *af; s32 low, high; s32 schedulerCount, workerCount, computeCount, workpileCount, allocatorCount, memoryCount; schedulerCount = read_range(dict, secname, "scheduler", &low, &high); workerCount = read_range(dict, secname, "worker", &low, &high); computeCount = read_range(dict, secname, "comptarget", &low, &high); workpileCount = read_range(dict, secname, "workpile", &low, &high); allocatorCount = read_range(dict, secname, "allocator", &low, &high); memoryCount = read_range(dict, secname, "memtarget", &low, &high); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "taskfactory"); INI_GET_STR (key, inststr, ""); tf = create_factory_task(inststr, NULL); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "tasktemplatefactory"); INI_GET_STR (key, inststr, ""); ttf = create_factory_tasktemplate(inststr, NULL); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "datablockfactory"); INI_GET_STR (key, inststr, ""); dbf = create_factory_datablock(inststr, NULL); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "eventfactory"); INI_GET_STR (key, inststr, ""); ef = create_factory_event(inststr, NULL); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "contextfactory"); INI_GET_STR (key, inststr, ""); cf = create_factory_context(inststr, NULL); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "sync"); INI_GET_STR (key, inststr, ""); lf = create_factory_lock(inststr, NULL); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "sync"); INI_GET_STR (key, inststr, ""); af = create_factory_atomic64(inststr, NULL); // Ugly but special case read_range(dict, secname, "guid", &low, &high); ASSERT (low == high); // We don't expect more than one guid provider gf = (ocrGuidProvider_t *)(all_instances[guid_type][low]); ASSERT (gf); ALLOC_PARAM_LIST(inst_param[j], paramListPolicyDomainInst_t); instance[j] = (ocrMappable_t *)((ocrPolicyDomainFactory_t *)factory)->instantiate(factory, schedulerCount, workerCount, computeCount, workpileCount, allocatorCount, memoryCount, tf, ttf, dbf, ef, cf, gf, lf, af, NULL, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created policy domain of index %d\n", j); setBootPD((ocrPolicyDomain_t *)instance[j]); } break; default: DPRINTF(DEBUG_LVL_WARN, "Error: %d index unexpected\n", index); break; } return 0; }
char* populate_type(ocrParamList_t **type_param, type_enum index, dictionary *dict, char *secname) { char *typestr; char key[MAX_KEY_SZ]; int value = 0; snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "name"); INI_GET_STR (key, typestr, ""); // TODO: populate type-specific fields as-needed; see compplatform_type for an example switch (index) { case guid_type: ALLOC_PARAM_LIST(*type_param, paramListGuidProviderFact_t); break; case memplatform_type: ALLOC_PARAM_LIST(*type_param, paramListMemPlatformFact_t); break; case memtarget_type: ALLOC_PARAM_LIST(*type_param, paramListMemTargetFact_t); break; case allocator_type: ALLOC_PARAM_LIST(*type_param, paramListAllocatorFact_t); break; case compplatform_type: { compPlatformType_t mytype = -1; TO_ENUM (mytype, typestr, compPlatformType_t, compplatform_types, compPlatformMax_id); switch (mytype) { case compPlatformPthread_id: { ALLOC_PARAM_LIST(*type_param, paramListCompPlatformPthread_t); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "stacksize"); INI_GET_INT (key, value, -1); ((paramListCompPlatformPthread_t *)(*type_param))->stackSize = (value==-1)?0:value; } break; default: ALLOC_PARAM_LIST(*type_param, paramListCompPlatformFact_t); break; } } break; case comptarget_type: ALLOC_PARAM_LIST(*type_param, paramListCompTargetFact_t); break; case workpile_type: ALLOC_PARAM_LIST(*type_param, paramListWorkpileFact_t); break; case worker_type: ALLOC_PARAM_LIST(*type_param, paramListWorkerFact_t); break; case scheduler_type: ALLOC_PARAM_LIST(*type_param, paramListSchedulerFact_t); break; case policydomain_type: ALLOC_PARAM_LIST(*type_param, paramListPolicyDomainFact_t); break; default: DPRINTF(DEBUG_LVL_WARN, "Error: %d index unexpected\n", index); break; } return strdup(typestr); }