예제 #1
0
static krb5_error_code
krb5_config_parse_debug (struct fileptr *f,
			 krb5_config_section **res,
			 unsigned *lineno,
			 const char **error_message)
{
    krb5_config_section *s = NULL;
    krb5_config_binding *b = NULL;
    char buf[BUFSIZ];
    krb5_error_code ret;

    while (config_fgets(buf, sizeof(buf), f) != NULL) {
	char *p;

	++*lineno;
	if(buf[strlen(buf) - 1] == '\n')
	    buf[strlen(buf) - 1] = '\0';
	p = buf;
	while(isspace((unsigned char)*p))
	    ++p;
	if (*p == '#' || *p == ';')
	    continue;
	if (*p == '[') {
	    ret = parse_section(p, &s, res, error_message);
	    if (ret) 
		return ret;
	    b = NULL;
	} else if (*p == '}') {
	    *error_message = "unmatched }";
	    return EINVAL;	/* XXX */
	} else if(*p != '\0') {
	    if (s == NULL) {
		*error_message = "binding before section";
		return EINVAL;
	    }
	    ret = parse_binding(f, lineno, p, &b, &s->u.list, error_message);
	    if (ret)
		return ret;
	}
    }
    return 0;
}
예제 #2
0
파일: vcd.c 프로젝트: adlerweb/libsigrok
static int format_match(const char *filename)
{
	FILE *file;
	gchar *name = NULL, *contents = NULL;
	gboolean status;
	
	file = fopen(filename, "r");
	if (file == NULL)
		return FALSE;

	/* If we can parse the first section correctly,
	 * then it is assumed to be a VCD file.
	 */
	status = parse_section(file, &name, &contents);
	status = status && (*name != '\0');
	
	g_free(name);
	g_free(contents);
	fclose(file);
	
	return status;
}
예제 #3
0
void
ResourceManager::parse(const std::string& section, FileReader& reader)
{
  if (reader.get_name() == "section")
  {
    parse_section(section, reader);
  }
  else if (reader.get_name() == "sprite")
  {
    std::string name;
    reader.read_string("name", name);
    if (!section.empty())
      name = section + "/" + name;
 
    resources[name].reset(new SpriteDescription(reader));
  }
  else if (reader.get_name() == "alias")
  {
    std::string name;
    std::string link;
    if (reader.read_string("name", name) &&
        reader.read_string("link", link))
    {
      //std::cout << "alias: " << name << " -> " << link << std::endl;
      aliases[name] = link;
    }
  }
  else if (reader.get_name() == "name")
  {
    // ignore (ugly)
  }
  else
  {
    std::cout << "ResourceManager: unknown token: '" << reader.get_name() << "'" << std::endl;
  }
}
예제 #4
0
파일: vcd.c 프로젝트: adlerweb/libsigrok
/* Parse VCD header to get values for context structure.
 * The context structure should be zeroed before calling this.
 */
static gboolean parse_header(FILE *file, struct context *ctx)
{
	uint64_t p, q;
	gchar *name = NULL, *contents = NULL;
	gboolean status = FALSE;
	struct probe *probe;

	while (parse_section(file, &name, &contents))
	{
		sr_dbg("Section '%s', contents '%s'.", name, contents);
	
		if (g_strcmp0(name, "enddefinitions") == 0)
		{
			status = TRUE;
			break;
		}
		else if (g_strcmp0(name, "timescale") == 0)
		{
			/* The standard allows for values 1, 10 or 100
			 * and units s, ms, us, ns, ps and fs. */
			if (sr_parse_period(contents, &p, &q) == SR_OK)
			{
				ctx->samplerate = q / p;
				if (q % p != 0)
				{
					/* Does not happen unless time value is non-standard */
					sr_warn("Inexact rounding of samplerate, %" PRIu64 " / %" PRIu64 " to %" PRIu64 " Hz.",
						q, p, ctx->samplerate);
				}
				
				sr_dbg("Samplerate: %" PRIu64, ctx->samplerate);
			}
			else
			{
				sr_err("Parsing timescale failed.");
			}
		}
		else if (g_strcmp0(name, "var") == 0)
		{
			/* Format: $var type size identifier reference $end */
			gchar **parts = g_strsplit_set(contents, " \r\n\t", 0);
			remove_empty_parts(parts);
			
			if (g_strv_length(parts) != 4)
			{
				sr_warn("$var section should have 4 items");
			}
			else if (g_strcmp0(parts[0], "reg") != 0 && g_strcmp0(parts[0], "wire") != 0)
			{
				sr_info("Unsupported signal type: '%s'", parts[0]);
			}
			else if (strtol(parts[1], NULL, 10) != 1)
			{
				sr_info("Unsupported signal size: '%s'", parts[1]);
			}
			else if (ctx->probecount >= ctx->maxprobes)
			{
				sr_warn("Skipping '%s' because only %d probes requested.", parts[3], ctx->maxprobes);
			}
			else
			{
				sr_info("Probe %d is '%s' identified by '%s'.", ctx->probecount, parts[3], parts[2]);
				probe = g_malloc(sizeof(struct probe));
				probe->identifier = g_strdup(parts[2]);
				probe->name = g_strdup(parts[3]);
				ctx->probes = g_slist_append(ctx->probes, probe);
				ctx->probecount++;
			}
			
			g_strfreev(parts);
		}
		
		g_free(name); name = NULL;
		g_free(contents); contents = NULL;
	}
	
	g_free(name);
	g_free(contents);
	
	return status;
}
예제 #5
0
static int
create_chips(struct rcfile *f, struct sim_chip **chips, int *cnt)
{
	struct sim_chip *chipsptr;
	int count, i;

	count = rc_getsectionscount(f, "chip");
	if (count > (MAX_CTRL_CS * MAX_SIM_DEV)) {
		error("Too many chip sections specified(%d)", count);
		return (ENOTSUP);
	} else if (count == 0) {
		error("No chip sections specified");
		return (ENOENT);
	}

	chipsptr = (struct sim_chip *)malloc(sizeof(struct sim_chip) * count);
	if (chipsptr == NULL) {
		error("Could not allocate memory for chip configuration");
		return (ENOMEM);
	}

	for (i = 0; i < count; i++) {
		bzero((void *)&chip_conf, sizeof(chip_conf));

		/*
		 * Bad block map have to end up with 0xffff, so
		 * we're filling array with 0xff. If bad block map is
		 * defined in config file, values will be overriden.
		 */
		memset((void *)&chip_conf.bad_block_map, 0xff,
		    sizeof(chip_conf.bad_block_map));

		if (validate_section_config(f, "chip", i) != 0) {
			free(chipsptr);
			return (EINVAL);
		}

		if (parse_section(f, "chip", i) != 0) {
			free(chipsptr);
			return (EINVAL);
		}

		memcpy(&chipsptr[i], &chip_conf, sizeof(chip_conf));

		/* Try to create chip with config parsed */
		debug("CHIP:\nNUM=%d\nCTRL_NUM=%d\nDEVID=%d\nMANID=%d\n"
		    "PAGE_SZ=%d\nOOBSZ=%d\nREAD_T=%d\nDEVMODEL=%s\n"
		    "MAN=%s\nCOLADDRCYCLES=%d\nROWADDRCYCLES=%d\nCHWIDTH=%d\n"
		    "PGS/BLK=%d\nBLK/LUN=%d\nLUNS=%d\nERR_RATIO=%d\n"
		    "WEARLEVEL=%d\nISWP=%d\n\n\n\n",
		    chipsptr[i].num, chipsptr[i].ctrl_num,
		    chipsptr[i].device_id, chipsptr[i].manufact_id,
		    chipsptr[i].page_size, chipsptr[i].oob_size,
		    chipsptr[i].read_time, chipsptr[i].device_model,
		    chipsptr[i].manufacturer, chipsptr[i].col_addr_cycles,
		    chipsptr[i].row_addr_cycles, chipsptr[i].width,
		    chipsptr[i].pgs_per_blk, chipsptr[i].blks_per_lun,
		    chipsptr[i].luns, chipsptr[i].error_ratio,
		    chipsptr[i].wear_level, chipsptr[i].is_wp);
	}
	*cnt = count;
	*chips = chipsptr;
	return (0);
}
예제 #6
0
파일: coroparse.c 프로젝트: hnkien/corosync
static int parse_section(FILE *fp,
                         char *path,
                         const char **error_string,
                         int depth,
                         parser_cb_f parser_cb,
                         icmap_map_t config_map,
                         void *user_data)
{
    char line[512];
    int i;
    char *loc;
    int ignore_line;
    char new_keyname[ICMAP_KEYNAME_MAXLEN];

    if (strcmp(path, "") == 0) {
        parser_cb("", NULL, NULL, PARSER_CB_START, error_string, config_map, user_data);
    }

    while (fgets (line, sizeof (line), fp)) {
        if (strlen(line) > 0) {
            if (line[strlen(line) - 1] == '\n')
                line[strlen(line) - 1] = '\0';
            if (strlen (line) > 0 && line[strlen(line) - 1] == '\r')
                line[strlen(line) - 1] = '\0';
        }
        /*
         * Clear out white space and tabs
         */
        for (i = strlen (line) - 1; i > -1; i--) {
            if (line[i] == '\t' || line[i] == ' ') {
                line[i] = '\0';
            } else {
                break;
            }
        }

        ignore_line = 1;
        for (i = 0; i < strlen (line); i++) {
            if (line[i] != '\t' && line[i] != ' ') {
                if (line[i] != '#')
                    ignore_line = 0;

                break;
            }
        }
        /*
         * Clear out comments and empty lines
         */
        if (ignore_line) {
            continue;
        }

        /* New section ? */
        if ((loc = strchr_rs (line, '{'))) {
            char *section = remove_whitespace(line, 1);

            loc--;
            *loc = '\0';

            if (strlen(path) + strlen(section) + 1 >= ICMAP_KEYNAME_MAXLEN) {
                *error_string = "parser error: Start of section makes total cmap path too long";
                return -1;
            }
            strcpy(new_keyname, path);
            if (strcmp(path, "") != 0) {
                strcat(new_keyname, ".");
            }
            strcat(new_keyname, section);

            if (!parser_cb(new_keyname, NULL, NULL, PARSER_CB_SECTION_START, error_string, config_map, user_data)) {
                return -1;
            }

            if (parse_section(fp, new_keyname, error_string, depth + 1, parser_cb, config_map, user_data))
                return -1;

            continue ;
        }

        /* New key/value */
        if ((loc = strchr_rs (line, ':'))) {
            char *key;
            char *value;

            *(loc-1) = '\0';
            key = remove_whitespace(line, 1);
            value = remove_whitespace(loc, 0);

            if (strlen(path) + strlen(key) + 1 >= ICMAP_KEYNAME_MAXLEN) {
                *error_string = "parser error: New key makes total cmap path too long";
                return -1;
            }
            strcpy(new_keyname, path);
            if (strcmp(path, "") != 0) {
                strcat(new_keyname, ".");
            }
            strcat(new_keyname, key);

            if (!parser_cb(new_keyname, key, value, PARSER_CB_ITEM, error_string, config_map, user_data)) {
                return -1;
            }

            continue ;
        }

        if (strchr_rs (line, '}')) {
            if (depth == 0) {
                *error_string = "parser error: Unexpected closing brace";

                return -1;
            }

            if (!parser_cb(path, NULL, NULL, PARSER_CB_SECTION_END, error_string, config_map, user_data)) {
                return -1;
            }

            return 0;
        }
    }

    if (strcmp(path, "") != 0) {
        *error_string = "parser error: Missing closing brace";
        return -1;
    }

    if (strcmp(path, "") == 0) {
        parser_cb("", NULL, NULL, PARSER_CB_END, error_string, config_map, user_data);
    }

    return 0;
}
예제 #7
0
파일: mustache.c 프로젝트: xles/journal
/**
 * Parses a string buffer for mustache tags.
 *
 * @param sds str  The string buffer to parse.
 *
 * @retval sds str  The parsed string buffer.
 */
static sds match_tags(sds str)
{
	puts("match_tags");
	sds re = sdsempty(), s = sdsempty(), buff = sdsempty(); 
	re = sdscatprintf(re, "(%s[\\S\\s]+?%s)", ldelim, rdelim);

	struct slre_cap caps[1];
	int i, sl = sdslen(str);

	struct slre_cap* match = slre_match_all(re, str, sl, caps, 1);
	int matches = slre_match_count(re, str, sl, caps, 1);
	
	sds remain = sdsdup(str);
	sds part = sdsempty();
	char *pos, *end, type;
	
	for (i=0; i < matches; i++) {
		s = sdsempty(); 
		s = sdscatprintf(s,"%.*s", match[i].len, match[i].ptr);
		
		if (!(pos = strstr(remain, s)))
			continue;

		part = sdscpylen(part, remain, pos-remain); 

		remain = sdscpy(remain, pos+sdslen(s)); 
	
		buff = sdscat(buff,part);

		type = s[(strlen(ldelim))];
		
		if (type == '#' || type == '^') {
			sds endtag = sdsdup(s);
			sds tag = sdscpylen( 
				sdsempty(), 
				( s + sdslen(ldelim) + 1 ),
				(
					sdslen(s) -
					sdslen(ldelim) - 
					sdslen(rdelim) - 1
				)
			);

			endtag[(sdslen(ldelim))] = '/';

			end = strstr(remain, endtag);
			printf("part: '%s'\n", part);
			
			s = sdscatlen(sdsempty(), remain, end-remain); 
			printf("search: '%s'\n", s);
			
			remain = sdscpy(remain, end+sdslen(endtag)); 
			printf("remain: '%s'\n", remain);

			//sds tmp = sdsempty();
			//tmp = match_tags(tmp);
			buff = parse_section(s, tag, type);
			//sdsfree(tmp);
			//buff = sdscat(buff, tmp);
		} else {
			buff = sdscat(buff,parse_tag(s));
			puts("foo");		
		}


	}

	buff = sdscat(buff,remain);
	str = sdscpylen(str,buff,sdslen(buff)); 
	
	puts("free part");
	sdsfree(part);
	puts("free remain");
	sdsfree(remain);
	puts("free buff");
	//sdsfree(buff);
	puts("free re");
	sdsfree(re);
	puts("free s");
//	sdsfree(s);
//	puts("return");
	return str;
}
예제 #8
0
int ruli_parse_message(ruli_parse_t *parse, ruli_msg_header_t *msg_hdr, 
		       const ruli_uint8_t *msg, int msg_len)
{
  const ruli_uint8_t *i;
  const ruli_uint8_t *j;
  const ruli_uint8_t *past_end;

  assert(!ruli_list_size(&parse->question_list));
  assert(!ruli_list_size(&parse->answer_list));
  assert(!ruli_list_size(&parse->authority_list));
  assert(!ruli_list_size(&parse->additional_list));

  parse->qdcount = msg_hdr->qdcount;
  parse->ancount = msg_hdr->ancount;
  parse->nscount = msg_hdr->nscount;
  parse->arcount = msg_hdr->arcount;

  /* Message too short? */
  if (msg_len < RULI_LIMIT_MSG_HEADER)
    return RULI_PARSE_SHORT_MSG;

  /* Skip message header */
  i = msg + RULI_LIMIT_MSG_HEADER;

  past_end = msg + msg_len;

  /*
   * Parse question section
   */
  j = parse_section(parse_question, 
		    &parse->question_list, i, past_end, parse->qdcount);
  if (!j)
    return RULI_PARSE_QUESTION;

  assert(i <= j);
  assert(j <= past_end);
  assert(ruli_list_size(&parse->question_list) == parse->qdcount);

  /*
   * Parse answer section
   */
  i = parse_section(parse_rr,
		    &parse->answer_list, j, past_end, parse->ancount);
  if (!i)
    return RULI_PARSE_ANSWER;

  assert(j <= i);
  assert(i <= past_end);
  assert(ruli_list_size(&parse->answer_list) == parse->ancount);

  /*
   * Parse authority section
   */
  j = parse_section(parse_rr,
		    &parse->authority_list, i, past_end, parse->nscount);
  if (!j)
    return RULI_PARSE_AUTHORITY;

  assert(i <= j);
  assert(j <= past_end);
  assert(ruli_list_size(&parse->authority_list) == parse->nscount);

  /*
   * Parse additional section
   */
  i = parse_section(parse_rr,
		    &parse->additional_list, j, past_end, parse->arcount);
  if (!i)
    return RULI_PARSE_ADDITIONAL;

  assert(j <= i);
  assert(i <= past_end);
  assert(ruli_list_size(&parse->additional_list) == parse->arcount);

  if (i < past_end)
    return RULI_PARSE_LONG_MSG;

  return RULI_PARSE_OK;
}
예제 #9
0
파일: config_win.c 프로젝트: vocho/openqnx
svn_error_t *
svn_config__parse_registry(svn_config_t *cfg, const char *file,
                           svn_boolean_t must_exist, apr_pool_t *pool)
{
  apr_pool_t *subpool;
  svn_stringbuf_t *section, *option, *value;
  svn_error_t *svn_err = SVN_NO_ERROR;
  HKEY base_hkey, hkey;
  DWORD index;
  LONG err;

  if (0 == strncmp(file, SVN_REGISTRY_HKLM, SVN_REGISTRY_HKLM_LEN))
    {
      base_hkey = HKEY_LOCAL_MACHINE;
      file += SVN_REGISTRY_HKLM_LEN;
    }
  else if (0 == strncmp(file, SVN_REGISTRY_HKCU, SVN_REGISTRY_HKCU_LEN))
    {
      base_hkey = HKEY_CURRENT_USER;
      file += SVN_REGISTRY_HKCU_LEN;
    }
  else
    {
      return svn_error_createf(SVN_ERR_BAD_FILENAME, NULL,
                               "Unrecognised registry path '%s'",
                               svn_path_local_style(file, pool));
    }

  err = RegOpenKeyEx(base_hkey, file, 0,
                     KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE,
                     &hkey);
  if (err != ERROR_SUCCESS)
    {
      const int is_enoent = APR_STATUS_IS_ENOENT(APR_FROM_OS_ERROR(err));
      if (!is_enoent)
        return svn_error_createf(SVN_ERR_BAD_FILENAME, NULL,
                                 "Can't open registry key '%s'",
                                 svn_path_local_style(file, pool));
      else if (must_exist && is_enoent)
        return svn_error_createf(SVN_ERR_BAD_FILENAME, NULL,
                                 "Can't find registry key '%s'",
                                 svn_path_local_style(file, pool));
      else
        return SVN_NO_ERROR;
    }


  subpool = svn_pool_create(pool);
  section = svn_stringbuf_create("", subpool);
  option = svn_stringbuf_create("", subpool);
  value = svn_stringbuf_create("", subpool);

  /* The top-level values belong to the [DEFAULT] section */
  svn_err = parse_section(cfg, hkey, SVN_CONFIG__DEFAULT_SECTION,
                          option, value);
  if (svn_err)
    goto cleanup;

  /* Now enumerate the rest of the keys. */
  svn_stringbuf_ensure(section, SVN_REG_DEFAULT_NAME_SIZE);
  for (index = 0; ; ++index)
    {
      DWORD section_len = section->blocksize;
      FILETIME last_write_time;
      HKEY sub_hkey;

      err = RegEnumKeyEx(hkey, index, section->data, &section_len,
                         NULL, NULL, NULL, &last_write_time);
      if (err == ERROR_NO_MORE_ITEMS)
          break;
      if (err == ERROR_MORE_DATA)
        {
          svn_stringbuf_ensure(section, section_len);
          err = RegEnumKeyEx(hkey, index, section->data, &section_len,
                             NULL, NULL, NULL, &last_write_time);
        }
      if (err != ERROR_SUCCESS)
        {
          svn_err =  svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
                                      "Can't enumerate registry keys");
          goto cleanup;
        }

      err = RegOpenKeyEx(hkey, section->data, 0,
                         KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE,
                         &sub_hkey);
      if (err != ERROR_SUCCESS)
        {
          svn_err =  svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
                                      "Can't open existing subkey");
          goto cleanup;
        }

      svn_err = parse_section(cfg, sub_hkey, section->data, option, value);
      RegCloseKey(sub_hkey);
      if (svn_err)
        goto cleanup;
    }

 cleanup:
  RegCloseKey(hkey);
  svn_pool_destroy(subpool);
  return svn_err;
}
예제 #10
0
파일: history.cpp 프로젝트: Botrix/pentago
// Compute rank-to-rank bandwidth estimates localized in time (dimensions: epoch,src,dst)
static Array<double,3> estimate_bandwidth(const vector<vector<Array<const history_t>>>& event_sorted_history,
                                          const int threads, const double dt_seconds) {
  Log::Scope scope("estimate bandwidth");
  GEODE_ASSERT(threads>1);
  const int ranks = CHECK_CAST_INT(event_sorted_history.size())/threads;
  GEODE_ASSERT((int)event_sorted_history.size()==ranks*threads);
  const double dt = 1e6*dt_seconds;
  // Count how many epochs we need
  int64_t elapsed = 0;
  for (auto& thread : event_sorted_history)
    for (auto& events : thread)
      if (events.size())
        elapsed = max(elapsed,events.back().end.us);
  const int epochs = int(ceil(elapsed/dt)); // Last epoch is incomplete
  // Statics: responses, outputs, total
  Vector<uint64_t,3> messages;
  Vector<double,3> total_data, total_time, max_time;
  int64_t max_time_travel = 0;
  const double compression_ratio = .35;
  // Traverse each large message, accumulating total data sent
  Array<double,3> bandwidths(epochs,ranks,ranks);
  for (const int target_rank : range(ranks))
    for (const int kind : vec(response_recv_kind,output_recv_kind))
      for (const history_t& target : event_sorted_history[threads*target_rank][kind]) {
        const auto deps = event_dependencies(event_sorted_history,-1,threads*target_rank,kind,target);
        GEODE_ASSERT(deps.size()==1);
        const int source_thread = deps[0].x;
        const int source_rank = source_thread/threads;
        GEODE_ASSERT(source_thread==source_rank*threads);
        const history_t& source = deps[0].z;
        const bool which = kind==output_recv_kind;
        messages[which]++;
        // Estimate message size
        const section_t section = parse_section(source.event);
        const Vector<uint8_t,4> block = parse_block(source.event);
        const double data_size = sizeof(Vector<super_t,2>)*block_shape(section.shape(),block).product()*(kind==response_recv_kind?compression_ratio:1);
        total_data[which] += data_size;
        // Distribute data amongst all overlapped epochs
        const int64_t time_travel = source.start.us - target.end.us;
        max_time_travel = max(max_time_travel,time_travel);
        Box<double> box(source.start.us/dt,target.end.us/dt);
        if (box.size()<=1e-7)
          box = Box<double>(box.center()).thickened(.5e-7);
        total_time[which] += box.size();
        max_time[which] = max(max_time[which],box.size());
        const double rate = data_size/box.size();
        for (const int epoch : range(max(0,int(box.min)),min(epochs,int(box.max)+1)))
          bandwidths(epoch,source_rank,target_rank) += rate*Box<double>::intersect(box,Box<double>(epoch,epoch+1)).size();
      }

  // Rescale
  bandwidths /= dt_seconds;

  // Print statistics
  cout << "dt = "<<dt_seconds<<" s"<<endl;
  cout << "elapsed = "<<1e-6*elapsed<<" s"<<endl;
  cout << "ranks = "<<ranks<<endl;
  messages[2] = messages.sum();
  total_data[2] = total_data.sum();
  total_time[2] = total_time.sum();
  max_time[2] = max_time.max();
  for (int i=0;i<3;i++) {
    cout << (i==0?"responses:":i==1?"outputs:":"total:") << endl;
    cout << "  messages = "<<messages[i]<<endl;
    cout << "  total data = "<<total_data[i]<<endl;
    cout << "  total time = "<<dt_seconds*total_time[i]<<endl;
    cout << "  average time = "<<dt_seconds*total_time[i]/messages[i]<<endl;
    cout << "  max time = "<<dt_seconds*max_time[i]<<endl;
    cout << "  average bandwidth = "<<total_data[i]/(1e-6*elapsed)<<endl;
    cout << "  average bandwidth / ranks = "<<total_data[i]/(1e-6*elapsed*ranks)<<endl;
  }
  cout << "max time travel = "<<1e-6*max_time_travel<<endl;
  cout << "bandwidth array stats:"<<endl;
  const double sum = bandwidths.sum();
  cout << "  sum = "<<sum<<endl;
  cout << "  average rank bandwidth = "<<sum/epochs/ranks<<endl;
  cout << "  average rank-to-rank bandwidth = "<<sum/epochs/sqr(ranks)<<endl;

  // All done
  return bandwidths;
}
예제 #11
0
파일: history.cpp 프로젝트: Botrix/pentago
// Collect message sizes and times, separating same-rank, same-node, and internode messages
static Hashtable<string,Array<const Vector<float,2>>>
message_statistics(const vector<vector<Array<const history_t>>>& event_sorted_history,
                   const int ranks_per_node, const int threads_per_rank,
                   const time_kind_t source_kind, const int steps, RawArray<const double> slice_compression_ratio) {
  GEODE_ASSERT(ranks_per_node>=1);
  GEODE_ASSERT(threads_per_rank>1);
  GEODE_ASSERT(vec(request_send_kind,response_send_kind,output_send_kind).contains(source_kind));
  const int ranks = CHECK_CAST_INT(event_sorted_history.size())/threads_per_rank;
  GEODE_ASSERT((int)event_sorted_history.size()==ranks*threads_per_rank);
  GEODE_ASSERT(slice_compression_ratio.size()==37);
  GEODE_ASSERT(steps==1 || steps==2);

  // Separate same-rank, same-node, and internode
  Vector<Array<Vector<float,2>>,3> data;

  // Traverse each message and place it in the appropriate bin
  for (const int source_rank : range(ranks)) {
    const int source_thread = source_rank*threads_per_rank;
    for (const history_t& source : event_sorted_history[source_thread][source_kind]) {
      auto deps = event_dependencies(event_sorted_history,1,source_thread,source_kind,source);
      GEODE_ASSERT(deps.size()==1);
      if (steps==2) {
        GEODE_ASSERT(source_kind == request_send_kind);
        deps = event_dependencies(event_sorted_history,1,deps[0].x,deps[0].y,deps[0].z);
        GEODE_ASSERT(deps.size()==1);
      }
      const int target_thread = deps[0].x;
      const int target_rank = target_thread/threads_per_rank;
      GEODE_ASSERT(target_thread==target_rank*threads_per_rank);
      const history_t& target = deps[0].z;

      // Clamp message time to be nonnegative
      const double time = max(0,target.start.seconds()-source.start.seconds());

      // Estimate message size
      double size;
      if (source_kind == request_send_kind)
        size = 8;
      else {
        const section_t section = parse_section(source.event);
        const Vector<uint8_t,4> block = parse_block(source.event);
        double compression_ratio = 1;
        if (source_kind == response_send_kind) {
          compression_ratio = slice_compression_ratio[section.sum()];
          GEODE_ASSERT(0<compression_ratio && compression_ratio<1);
        }
        size = sizeof(Vector<super_t,2>)*block_shape(section.shape(),block).product()*compression_ratio;
      }

      // Add entry
      const int type = source_rank==target_rank                               ? 0
                     : source_rank/ranks_per_node==target_rank/ranks_per_node ? 1
                                                                              : 2;
      data[type].append(Vector<float,2>(size,time));
    }
  }

  // Make a nice hashtable for Python
  Hashtable<string,Array<const Vector<float,2>>> table;
  table["same-rank"] = data[0];
  table["same-node"] = data[1];
  table["different"] = data[2];
  return table;
}
예제 #12
0
파일: history.cpp 프로젝트: Botrix/pentago
static Array<Tuple<time_kind_t,event_t>> dependencies(const int direction, const time_kind_t kind, const event_t event) {
  GEODE_ASSERT(abs(direction)==1);
  static_assert(compress_kind==0,"Verify that -kind != kind for kinds we care about");

  // Parse event
  const section_t section = parse_section(event);
  const auto block = parse_block(event);
  const uint8_t dimensions = parse_dimensions(event),
                parent_to_child_symmetry = dimensions>>2,
                dimension = dimensions&3;
  const auto ekind = event&ekind_mask;

  // See mpi/graph for summarized explanation
  Array<Tuple<time_kind_t,event_t>> deps;
  switch (direction*kind) {
    case -allocate_line_kind: {
      GEODE_ASSERT(ekind==line_ekind);
      break; }
    case  response_recv_kind:
    case -request_send_kind: {
      GEODE_ASSERT(ekind==block_lines_ekind);
      const auto other_kind = kind==response_recv_kind ? schedule_kind : allocate_line_kind;
      const auto parent_section = section.parent(dimension).transform(symmetry_t::invert_global(parent_to_child_symmetry));
      const auto permutation = section_t::quadrant_permutation(parent_to_child_symmetry);
      const uint8_t parent_dimension = permutation.find(dimension);
      const auto block_base = Vector<uint8_t,4>(block.subset(permutation)).remove_index(parent_dimension);
      deps.append(tuple(other_kind,line_event(parent_section,parent_dimension,block_base)));
      break; }
    case  request_send_kind: {
      GEODE_ASSERT(ekind==block_lines_ekind);
      deps.append(tuple(response_send_kind,event));
      break; }
    case -response_send_kind:
    case  response_send_kind: {
      GEODE_ASSERT(ekind==block_lines_ekind);
      deps.append(tuple(direction<0?request_send_kind:response_recv_kind,event));
      break; }
    case -response_recv_kind: {
      GEODE_ASSERT(ekind==block_lines_ekind);
      deps.append(tuple(response_send_kind,event));
      break; }
    case  allocate_line_kind:
    case -schedule_kind: {
      GEODE_ASSERT(ekind==line_ekind);
      if (section.sum()!=35) {
        const auto other_kind = kind==allocate_line_kind ? request_send_kind : response_recv_kind;
        const auto child_section = section.child(dimension).standardize<8>();
        const auto permutation = section_t::quadrant_permutation(symmetry_t::invert_global(child_section.y));
        const uint8_t child_dimension = permutation.find(dimension);
        const dimensions_t dimensions(child_section.y,child_dimension);
        auto child_block = Vector<uint8_t,4>(block.slice<0,3>().insert(0,dimension).subset(permutation));
        for (const uint8_t b : range(section_blocks(child_section.x)[child_dimension])) {
          child_block[child_dimension] = b;
          deps.append(tuple(other_kind,block_lines_event(child_section.x,dimensions,child_block)));
        }
      }
      break; }
    case  schedule_kind: {
      GEODE_ASSERT(ekind==line_ekind);
      deps.append(tuple(compute_kind,event)); // Corresponds to many different microline compute events
      break; }
    case -compute_kind: // Note: all microline compute events have the same line event
    case  compute_kind: {
      GEODE_ASSERT(ekind==line_ekind);
      deps.append(tuple(direction<0?schedule_kind:wakeup_kind,event));
      break; }
    case -wakeup_kind: {
      GEODE_ASSERT(ekind==line_ekind);
      deps.append(tuple(compute_kind,event)); // Corresponds to many different microline compute events
      break; }
    case  wakeup_kind: {
      GEODE_ASSERT(ekind==line_ekind);
      const auto block_base = block.slice<0,3>();
      for (const uint8_t b : range(section_blocks(section)[dimension]))
        deps.append(tuple(output_send_kind,block_line_event(section,dimension,block_base.insert(b,dimension))));
      break; }
    case -output_send_kind:
    case  output_send_kind: {
      GEODE_ASSERT(ekind==block_line_ekind);
      if (direction<0)
        deps.append(tuple(wakeup_kind,line_event(section,dimension,block.remove_index(dimension))));
      else
        deps.append(tuple(output_recv_kind,event));
      break; }
    case -output_recv_kind:
    case  output_recv_kind: {
      GEODE_ASSERT(ekind==block_line_ekind);
      deps.append(tuple(direction<0?output_send_kind:snappy_kind,event));
      break; }
    case -snappy_kind:
    case  snappy_kind: {
      GEODE_ASSERT(ekind==block_line_ekind);
      if (direction<0)
        deps.append(tuple(output_recv_kind,event));
      break; }
    default:
      break;
  }
  return deps;
}
예제 #13
0
int parse_yasm_labels(struct label *l, const struct text *t)
{
	int errcode, no_org_directive;
	size_t i;
	uint64_t base_addr;
	enum { linelen = 1024 };
	char line[linelen];
	struct label *length;

	if (bug_on(!t))
		return -err_internal;

	base_addr = 0;
	no_org_directive = 1;
	length = NULL;

	/* determine base address from org directive and insert special
	 * section labels.
	 */
	for (i = 0; i < t->n; i++) {
		char *tmp;

		errcode = text_line(t, line, linelen, i);
		if (errcode < 0)
			return errcode;

		tmp = strstr(line, "[section");
		if (tmp) {
			tmp += strlen("[section");
			errcode = parse_section(tmp, l, &length);
			if (errcode < 0)
				return errcode;
			continue;
		}

		tmp = strstr(line, "[org");
		if (tmp) {
			base_addr = strtol(tmp+strlen("[org"), NULL, 0);

			errcode = l_append(l, "org", base_addr);
			if (errcode < 0)
				return errcode;

			no_org_directive = 0;
			continue;
		}

		/* update the section_<name>_length label, if we have one.
		 *
		 * this must be last; it destroys @line.
		 */
		if (length) {
			uint64_t value, size;

			tmp = strtok(line, " ");
			if (!tmp)
				continue;

			/* we expect a line number. */
			errcode = str_to_uint64(tmp, &value, 10);
			if (errcode < 0)
				continue;

			tmp = strtok(NULL, " ");
			if (!tmp)
				continue;

			/* we expect an address. */
			errcode = str_to_uint64(tmp, &value, 16);
			if (errcode < 0)
				continue;

			tmp = strtok(NULL, " ");
			if (!tmp)
				continue;

			/* we expect an opcode. */
			errcode = str_to_uint64(tmp, &value, 16);
			if (errcode < 0)
				continue;

			/* we got an opcode - let's compute it's size. */
			for (size = 0; value != 0; value >>= 8)
				size += 1;

			/* update the section_<name>_length label. */
			length->addr += size;
		}
	}

	if (no_org_directive)
		return -err_no_org_directive;

	for (i = 0; i < t->n; i++) {
		char *tmp, *name;
		uint64_t addr;

		errcode = text_line(t, line, linelen, i);
		if (errcode < 0)
			goto error;

		/* Change the base on section switches. */
		tmp = strstr(line, "[section");
		if (tmp) {
			tmp += strlen("[section");
			errcode = lookup_section_vstart(l, tmp, &base_addr);
			if (errcode < 0)
				return errcode;
			continue;
		}

		/* skip line number count.  */
		tmp = strtok(line, " ");
		if (!tmp)
			continue;

		/* the label can now be on the same line as the memory
		 * address or on a line by its own.
		 * we look at the next token and (1) if it looks like a
		 * label, we search in the following lines for the
		 * corresponding address; or (2) if it looks like an
		 * address, we store it and see if the token after the
		 * opcode looks like a token; or (3) none of the above,
		 * we continue with the next line.
		 */

		/* second token after the line number count.  it's
		 * either an address; or a label.
		 */
		tmp = strtok(NULL, " ");
		if (!tmp)
			continue;

		if (!make_label(tmp)) {
			/* get address in case we find a label later.  */
			if (sscanf(tmp, "%" PRIx64, &addr) != 1)
				continue;

			/* skip the opcode token.  */
			tmp = strtok(NULL, " ");
			if (!tmp)
				continue;

			/* this might be a label now.  */
			tmp = strtok(NULL, " ");
			if (!make_label(tmp))
				continue;

			errcode = l_append(l, tmp, addr + base_addr);
			if (errcode < 0)
				goto error;
			continue;
		}
		name = duplicate_str(tmp);
		if (!name) {
			errcode = -err_no_mem;
			goto error;
		}

		/* there was a label so now an address needs to
		 * be found.
		 */
		errcode = -err_label_addr;
		for (i += 1; i < t->n; i++) {
			int errcode_text;

			errcode_text = text_line(t, line, linelen, i);
			if (errcode_text < 0) {
				errcode = errcode_text;
				break;
			}
			if (sscanf(line, "%*d %" PRIx64 " %*x %*s", &addr)
			    == 1) {
				errcode = l_append(l, name, addr + base_addr);
				break;
			}
		}
		if (errcode == -err_label_addr)
			fprintf(stderr, "label '%s' has no address\n", name);
		free(name);
		if (errcode < 0)
			goto error;
	}

	return 0;

error:
	l_free(l->next);
	free(l->name);
	l->next = NULL;
	l->name = NULL;
	return errcode;
}
예제 #14
0
/*
 * Read configuration file and setup target parameters.
 */
void target_configure()
{
    FILE *fp;
    int c;

    /*
     * Find the configuration file, if any.
     * (1) First, try a path from PIC32PROG_CONF_FILE environment variable.
     * (2) Otherwise, look for a file in the local directory.
     * (3) Otherwise, use /usr/local/etc/ directory (on Unix)
     *     or a directory where pic32prog.exe resides (on Windows)
     */
    confname = getenv("PIC32PROG_CONF_FILE");
    if (! confname)
        confname = "pic32prog.conf";

    if (access(confname, 0) < 0) {
#if defined(__CYGWIN32__) || defined(MINGW32)
        char *p = strrchr(progname, '\\');
        if (p) {
            char *buf = malloc(p - progname + 16);
            if (! buf) {
                fprintf(stderr, "%s: out of memory\n", progname);
                exit(-1);
            }
            strncpy(buf, progname, p - progname);
            strcpy(buf + (p - progname), "\\pic32prog.conf");
            confname = buf;
        } else
            confname = "c:\\pic32prog.conf";
#else
        confname = "/usr/local/etc/pic32prog.conf";
#endif
    }

    fp = fopen(confname, "r");
    if (! fp) {
        /* No config file available: that's OK. */
        return;
    }
    bsize = 1024;
    bufr = (char*) malloc(bsize);
    if (! bufr) {
        fprintf(stderr, "%s: malloc failed\n", confname);
        fclose(fp);
        exit(-1);
    }

    /* Parse file. */
    c = eat_whitespace(fp);
    while (c > 0) {
        switch (c) {
        case '\n':          /* blank line */
            c = eat_whitespace(fp);
            break;

        case ';':           /* comment line */
        case '#':
            c = eat_comment(fp);
            break;

        case '[':           /* section header */
            parse_section(fp);
            c = eat_whitespace(fp);
            break;

        case '\\':          /* bogus backslash */
            c = eat_whitespace(fp);
            break;

        default:            /* parameter line */
            parse_parameter(fp, configure_parameter, c);
            c = eat_whitespace(fp);
            break;
        }
    }
    configure_parameter("", 0, 0); /* Final call: end of file. */
    fclose(fp);
    if (cursec) {
        free(cursec);
        cursec = 0;
    }
    free(bufr);
    bufr = 0;
    bsize = 0;
}
예제 #15
0
static struct timemaster_config *config_parse(char *path)
{
	struct timemaster_config *config = xcalloc(1, sizeof(*config));
	FILE *f;
	char buf[4096], *line, *section_name = NULL;
	char **section_lines = NULL;
	int ret = 0;

	config->sources = (struct source **)parray_new();
	config->ntp_program = DEFAULT_NTP_PROGRAM;
	config->rundir = xstrdup(DEFAULT_RUNDIR);
	config->first_shm_segment = DEFAULT_FIRST_SHM_SEGMENT;

	init_program_config(&config->chronyd, "chronyd",
			    NULL, DEFAULT_CHRONYD_SETTINGS, NULL);
	init_program_config(&config->ntpd, "ntpd",
			    NULL, DEFAULT_NTPD_SETTINGS, NULL);
	init_program_config(&config->phc2sys, "phc2sys",
			    DEFAULT_PHC2SYS_OPTIONS, NULL, NULL);
	init_program_config(&config->ptp4l, "ptp4l",
			    DEFAULT_PTP4L_OPTIONS, NULL, NULL);

	f = fopen(path, "r");
	if (!f) {
		pr_err("failed to open %s: %m", path);
		free(config);
		return NULL;
	}

	while (fgets(buf, sizeof(buf), f)) {
		/* remove trailing and leading whitespace */
		for (line = buf + strlen(buf) - 1;
		     line >= buf && isspace(*line); line--)
			*line = '\0';
		for (line = buf; *line && isspace(*line); line++)
			;
		/* skip comments and empty lines */
		if (!*line || *line == '#')
			continue;

		if (*line == '[') {
			/* parse previous section before starting another */
			if (section_name) {
				if (parse_section(section_lines, section_name,
						  config)) {
					ret = 1;
					break;
				}
				free_parray((void **)section_lines);
				free(section_name);
			}
			section_name = parse_section_name(line);
			section_lines = (char **)parray_new();
			continue;
		}

		if (!section_lines) {
			pr_err("settings outside section");
			ret = 1;
			break;
		}

		parray_append((void ***)&section_lines, xstrdup(line));
	}

	if (!ret && section_name &&
	    parse_section(section_lines, section_name, config)) {
		ret = 1;
	}

	fclose(f);

	if (section_name)
		free(section_name);
	if (section_lines)
		free_parray((void **)section_lines);

	if (ret) {
		config_destroy(config);
		return NULL;
	}

	return config;
}
예제 #16
0
/*
 * value = TOK_STRING | TOK_INTEGER | TOK_BOOLEAN | TOK_KEYWORD
 * >> value (',' value)* '}'
 */
static int parse_array_or_section (section_t **section,scan_t *sc,char **keyword)
{
   int result,saved = 0;
   stmt_t *nw = NULL;

   SCAN();

   if (result == TOK_KEYWORD)
	 {
		char *tmp = sc->token.keyword;

		if ((result = scan (sc)) < 0)
		  {
			 mem_free (tmp);
			 return (ERR);
		  }

		saved = result;

		if (result == '=')
		  {
			 if ((*section)->child == NULL)
			   {
				  if (((*section)->child = (section_t **) mem_alloc (sizeof (section_t *))) == NULL)
					{
out_of_memory:
					   log_printf (LOG_ERROR,"Out of memory\n");
					   mem_free (tmp);
					   return (ERR);
					}

				  if (((*section)->child[0] = (section_t *) mem_alloc (sizeof (section_t))) == NULL)
					{
					   mem_free ((*section)->child);
					   (*section)->child = NULL;
					   goto out_of_memory;
					}
			   }
			 else
			   {
				  section_t **ptr;

				  if ((ptr = (section_t **) mem_realloc ((*section)->child,((*section)->n + 1) * sizeof (section_t *))) == NULL)
					goto out_of_memory;

				  (*section)->child = ptr;

				  if (((*section)->child[(*section)->n] = (section_t *) mem_alloc (sizeof (section_t))) == NULL)
					goto out_of_memory;
			   }

			 (*section)->child[(*section)->n]->n = 0;
			 (*section)->child[(*section)->n]->name = *keyword;
			 (*section)->child[(*section)->n]->stmt = NULL;
			 (*section)->child[(*section)->n]->child = NULL;

			 ((*section)->n)++;

			 return (parse_section (&(*section)->child[(*section)->n - 1],sc,&tmp));
		  }

		result = TOK_KEYWORD;
		sc->token.keyword = tmp;
	 }

   if (result != TOK_STRING && result != TOK_INTEGER && result != TOK_BOOLEAN && result != TOK_KEYWORD)
	 {
		mem_free (*keyword);
missing_value:
		parse_error (sc,"TOK_STRING, TOK_INTEGER, TOK_BOOLEAN, or TOK_ENUM",result);
		goto error;
	 }

   if ((nw = stmt_init (keyword,result)) == NULL)
	 {
unable_to_add:
		if (result == TOK_STRING)
		  mem_free (sc->token.string);
		else if (result == TOK_KEYWORD)
		  mem_free (sc->token.keyword);
		goto error;
	 }

   if (stmt_add (nw,sc) < 0)
	 goto unable_to_add;

   if (result == TOK_KEYWORD)
	 result = saved;
   else if ((result = scan (sc)) < 0)
	 goto error;

   while (result == ',')
	 {
		if ((result = scan (sc)) < 0) goto error;

		if (result != TOK_STRING && result != TOK_INTEGER && result != TOK_BOOLEAN && result != TOK_KEYWORD)
		  goto missing_value;

		if (result != nw->type && (result != TOK_KEYWORD || nw->type != TOK_ENUM))
		  {
			 type_mismatch (sc,nw->type,result);
			 goto error;
		  }

		if (stmt_add (nw,sc) < 0)
		  goto unable_to_add;

		if ((result = scan (sc)) < 0) goto error;
	 }

   if (result != '}')
	 {
		parse_error (sc,"'}'",result);
		goto error;
	 }

   stmt_save (*section,&nw);

   return (OK);

error:
   if (nw != NULL) stmt_destroy (&nw);
   return (ERR);
}
예제 #17
0
int main(int argc, char *argv[])
{
	int fd;
	int exit_code = EXIT_SUCCESS;
	struct stat st;
	unsigned int size;
	uint8_t *buffer;
	uint8_t *buffer_iterator;
	struct actions_firmware_header header;
	struct actions_firmware_section section;
	unsigned int i = 0;
	int ret = 1;
	int err = 0;
	int dump = 1;
	const char *path_prefix;

	if (argc != 3) {
		usage(argv[0]);
		exit(1);
	}

	path_prefix = argv[2];
	err = mkdir(path_prefix, 0755);
	if (err < 0) {
		perror("mkdir");
		exit_code = EXIT_FAILURE;
		goto out;
	}

	fd = open(argv[1], O_RDONLY);
	if (fd < 0) {
		perror("open");
		exit_code = EXIT_FAILURE;
		goto out;
	}
	if (fstat(fd, &st) < 0) {
		perror("fstat");
		exit_code = EXIT_FAILURE;
		goto out_close_fd;
	}
	size = st.st_size;

	buffer = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
	if (buffer == NULL) {
		perror("mmap");
		exit_code = EXIT_FAILURE;
		goto out_close_fd;
	}

	if (size < ACTIONS_HEADER_DISK_SIZE) {
		fprintf(stderr, "File cannot contain an Actions firmware header.\n");
		exit_code = EXIT_FAILURE;
		goto out_munmap;
	}

	/* iterate over a copy of the pointer */
	buffer_iterator = buffer;

	err = parse_header(&buffer_iterator, &header);
	if (err) {
		fprintf(stderr, "Cannot parse the header.\n");
		exit_code = EXIT_FAILURE;
		goto out_munmap;
	}

	print_header(&header);

	while((err = parse_section(&buffer_iterator, &section)) == 0) {
		print_section(&section);
		i++;

		if (strncmp((char *)section.name, "FIRM", 16) == 0) {
			uint8_t *firm_buffer_iterator = buffer + section.start_address + 0x200;
			struct actions_firmware_section firm_section;
			struct actions_firmware_section prev_section = {
				.start_address = section.start_address + 0x2000,
				.length = 0,
			};
			while((err = parse_section(&firm_buffer_iterator, &firm_section)) == 0) {

				/*
				 * unknown1 seems to be some form of checksum for
				 * firm sections, if a sections have the same
				 * checksum of the previous on they are not
				 * duplicated but refer to the same memory
				 * region, so do not increase che start
				 * address
				 */
				if (firm_section.unknown1 == prev_section.unknown1) {
					firm_section.start_address = prev_section.start_address;
				} else {
					firm_section.start_address = prev_section.start_address + prev_section.length;
				}

				printf("\t");
				print_section(&firm_section);

				if (dump)
					dump_section(path_prefix, "FIRM_", buffer, &firm_section);

				prev_section = firm_section;
			}
		} else if (strncmp((char *)section.name, "LINUX", 16) == 0) {
			continue;
		}

		if (dump)
			dump_section(path_prefix, "", buffer, &section);

	}