Exemple #1
0
/**
 * replicates a local dialog delete event to all the destinations
 * specified with the 'replicate_dialogs' modparam
 */
void replicate_dialog_deleted(struct dlg_cell *dlg)
{
	int rc;
	bin_packet_t packet;

	if (bin_init(&packet, &dlg_repl_cap, REPLICATION_DLG_DELETED, BIN_VERSION, 1024) != 0)
		goto error;

	bin_push_str(&packet, &dlg->callid);
	bin_push_str(&packet, &dlg->legs[DLG_CALLER_LEG].tag);
	bin_push_str(&packet, &dlg->legs[callee_idx(dlg)].tag);

	rc = clusterer_api.send_all(&packet, dialog_repl_cluster);
	switch (rc) {
	case CLUSTERER_CURR_DISABLED:
		LM_INFO("Current node is disabled in cluster: %d\n", dialog_repl_cluster);
		goto error_free;
	case CLUSTERER_DEST_DOWN:
		LM_ERR("All destinations in cluster: %d are down or probing\n",
			dialog_repl_cluster);
		goto error_free;
	case CLUSTERER_SEND_ERR:
		LM_ERR("Error sending in cluster: %d\n", dialog_repl_cluster);
		goto error_free;
	}

	if_update_stat(dlg_enable_stats, delete_sent, 1);
	bin_free_packet(&packet);
	return;
error_free:
	bin_free_packet(&packet);
error:
	LM_ERR("Failed to replicate deleted dialog\n");
}
Exemple #2
0
t_bin	read_bin(char *file)
{
	t_bin			bin;
	int				rd;
	unsigned char	*buff;
	int				fd;

	bin_init(&bin);
	if ((fd = open(file, O_RDONLY)) == -1)
	{
		ft_putendl("Failed to open file");
		exit(-1);
	}
	if (!(buff = malloc(sizeof(*buff) * 100)))
	{
		ft_putendl("Failed to malloc read buffer");
		exit(-1);
	}
	while ((rd = read(fd, buff, 100)) > 0)
		bin_append(&bin, buff, rd);
	if (rd == -1)
	{
		ft_putendl("Error while reading file");
		exit(-1);
	}
	free(buff);
	return (bin);
}
Exemple #3
0
void replicate_lb_status(struct lb_dst *dst)
{
	bin_packet_t packet;
	int rc;

	if (bin_init(&packet, &status_repl_cap, REPL_LB_STATUS_UPDATE, BIN_VERSION, 0)!=0){
		LM_ERR("failed to replicate this event\n");
		return;
	}

	bin_push_int(&packet, dst->group);
	bin_push_str(&packet, &dst->uri);
	bin_push_int(&packet, dst->flags&LB_DST_STAT_MASK);

	rc = clusterer_api.send_all(&packet, lb_repl_cluster);
	switch (rc) {
	case CLUSTERER_CURR_DISABLED:
		LM_INFO("Current node is disabled in cluster: %d\n", lb_repl_cluster);
		break;
	case CLUSTERER_DEST_DOWN:
		LM_INFO("All destinations in cluster: %d are down or probing\n",
			lb_repl_cluster);
		break;
	case CLUSTERER_SEND_ERR:
		LM_ERR("Error sending in cluster: %d\n", lb_repl_cluster);
		break;
	}

	bin_free_packet(&packet);
}
Exemple #4
0
int send_shtag_active_info(str *tag_name, int node_id)
{
	bin_packet_t packet;

	if (bin_init(&packet, &dlg_repl_cap, DLG_SHARING_TAG_ACTIVE, BIN_VERSION,
		0) < 0) {
		LM_ERR("Failed to init bin packet");
		return -1;
	}
	bin_push_str(&packet, tag_name);

	if (node_id) {
		if (clusterer_api.send_to(&packet, dialog_repl_cluster, node_id) !=
			CLUSTERER_SEND_SUCCES) {
			bin_free_packet(&packet);
			return -1;
		}
	} else
		if (clusterer_api.send_all(&packet, dialog_repl_cluster) !=
			CLUSTERER_SEND_SUCCES) {
			bin_free_packet(&packet);
			return -1;
		}

	bin_free_packet(&packet);

	return 0;
}
Exemple #5
0
/**
 * replicates a local dialog update to all the destinations
 * specified with the 'replicate_dialogs' modparam
 */
void replicate_dialog_updated(struct dlg_cell *dlg)
{
	struct replication_dest *d;
	static str module_name = str_init("dialog");
	int callee_leg;
	str *vars, *profiles;

	if (bin_init(&module_name, REPLICATION_DLG_UPDATED) != 0)
		goto error;

	callee_leg = callee_idx(dlg);

	bin_push_str(&dlg->callid);
	bin_push_str(&dlg->legs[DLG_CALLER_LEG].tag);
	bin_push_str(&dlg->legs[callee_leg].tag);

	bin_push_str(&dlg->from_uri);
	bin_push_str(&dlg->to_uri);

	bin_push_int(dlg->h_id);
	bin_push_int(dlg->start_ts);
	bin_push_int(dlg->state);

	bin_push_str(&dlg->legs[DLG_CALLER_LEG].bind_addr->sock_str);
	if (dlg->legs[callee_leg].bind_addr)
		bin_push_str(&dlg->legs[callee_leg].bind_addr->sock_str);
	else
		bin_push_str(NULL);

	bin_push_str(&dlg->legs[DLG_CALLER_LEG].r_cseq);
	bin_push_str(&dlg->legs[callee_leg].r_cseq);
	bin_push_str(&dlg->legs[DLG_CALLER_LEG].route_set);
	bin_push_str(&dlg->legs[callee_leg].route_set);
	bin_push_str(&dlg->legs[DLG_CALLER_LEG].contact);
	bin_push_str(&dlg->legs[callee_leg].contact);
	bin_push_str(&dlg->legs[callee_leg].from_uri);
	bin_push_str(&dlg->legs[callee_leg].to_uri);

	/* XXX: on shutdown only? */
	vars = write_dialog_vars(dlg->vals);
	profiles = write_dialog_profiles(dlg->profile_links);

	bin_push_str(vars);
	bin_push_str(profiles);
	bin_push_int(dlg->user_flags);
	bin_push_int(dlg->flags &
			     ~(DLG_FLAG_NEW|DLG_FLAG_CHANGED|DLG_FLAG_VP_CHANGED));
	bin_push_int((unsigned int)time(0) + dlg->tl.timeout - get_ticks());
	bin_push_int(dlg->legs[DLG_CALLER_LEG].last_gen_cseq);
	bin_push_int(dlg->legs[callee_leg].last_gen_cseq);

	for (d = replication_dests; d; d = d->next)
		bin_send(&d->to);

	if_update_stat(dlg_enable_stats,update_sent,1);
	return;

error:
	LM_ERR("Failed to replicate updated dialog\n");
}
Exemple #6
0
/**
 * replicates a locally created dialog to all the destinations
 * specified with the 'replicate_dialogs' modparam
 */
void replicate_dialog_created(struct dlg_cell *dlg)
{
	int rc;
	bin_packet_t packet;

	dlg_lock_dlg(dlg);

	if (dlg->state != DLG_STATE_CONFIRMED_NA && dlg->state != DLG_STATE_CONFIRMED) {
		/* we don't need to replicate when in deleted state */
		LM_WARN("not replicating dlg create message due to bad state %d (%.*s)\n",
			dlg->state, dlg->callid.len, dlg->callid.s);
		goto no_send;
	}

	if (dlg->replicated) {
		/* already created - must be a retransmission */
		LM_DBG("not replicating retransmission for %p (%.*s)\n",
			dlg, dlg->callid.len, dlg->callid.s);
		goto no_send;
	}

	if (bin_init(&packet, &dlg_repl_cap, REPLICATION_DLG_CREATED, BIN_VERSION, 0) != 0)
		goto init_error;

	bin_push_dlg(&packet, dlg);

	dlg->replicated = 1;

	dlg_unlock_dlg(dlg);

	rc = clusterer_api.send_all(&packet, dialog_repl_cluster);
	switch (rc) {
	case CLUSTERER_CURR_DISABLED:
		LM_INFO("Current node is disabled in cluster: %d\n", dialog_repl_cluster);
		goto error;
	case CLUSTERER_DEST_DOWN:
		LM_INFO("All destinations in cluster: %d are down or probing\n",
			dialog_repl_cluster);
		goto error;
	case CLUSTERER_SEND_ERR:
		LM_ERR("Error sending in cluster: %d\n", dialog_repl_cluster);
		goto error;
	}

	if_update_stat(dlg_enable_stats,create_sent,1);
	bin_free_packet(&packet);
	return;

error:
	bin_free_packet(&packet);
	LM_ERR("Failed to replicate created dialog\n");
	return;

init_error:
	LM_ERR("Failed to replicate created dialog\n");
no_send:
	dlg_unlock_dlg(dlg);
	return;
}
Exemple #7
0
/**
 * replicates a local dialog update in the cluster
 */
void replicate_dialog_updated(struct dlg_cell *dlg)
{
	int rc;
	bin_packet_t packet;


	dlg_lock_dlg(dlg);
	if (dlg->state == DLG_STATE_DELETED) {
		/* we no longer need to update anything */
		LM_WARN("not replicating dlg update message due to bad state %d (%.*s)\n",
			dlg->state, dlg->callid.len, dlg->callid.s);
		goto end;
	}

	if (bin_init(&packet, &dlg_repl_cap, REPLICATION_DLG_UPDATED, BIN_VERSION, 0) != 0)
		goto init_error;

	bin_push_dlg(&packet, dlg);

	dlg->replicated = 1;

	dlg_unlock_dlg(dlg);

	rc = clusterer_api.send_all(&packet, dialog_repl_cluster);
	switch (rc) {
	case CLUSTERER_CURR_DISABLED:
		LM_INFO("Current node is disabled in cluster: %d\n", dialog_repl_cluster);
		goto error;
	case CLUSTERER_DEST_DOWN:
		LM_ERR("All destinations in cluster: %d are down or probing\n",
			dialog_repl_cluster);
		goto error;
	case CLUSTERER_SEND_ERR:
		LM_ERR("Error sending in cluster: %d\n", dialog_repl_cluster);
		goto error;
	}

	if_update_stat(dlg_enable_stats,update_sent,1);
	bin_free_packet(&packet);
	return;

error:
	LM_ERR("Failed to replicate updated dialog\n");
	bin_free_packet(&packet);
	return;

init_error:
	LM_ERR("Failed to replicate updated dialog\n");
end:
	dlg_unlock_dlg(dlg);
	return;
}
int repl_prof_remove(str *name, str *value)
{
	static str module_name = str_init("dialog");
	if (!repl_prof_dests_nr)
		return 0;
	if (bin_init(&module_name, REPLICATION_DLG_PROFILE, BIN_VERSION) < 0) {
		LM_ERR("cannot initiate bin buffer\n");
		return -1;
	}
	if (repl_prof_add(name, value?1:0, value, 0) < 0)
		return -1;
	dlg_replicate_profiles();
	return 0;
}
Exemple #9
0
int repl_prof_remove(str *name, str *value)
{
	static str module_name = str_init("dialog");
	if (profile_replicate_cluster <= 0)
		return 0;
	if (bin_init(&module_name, REPLICATION_DLG_PROFILE, BIN_VERSION) < 0) {
		LM_ERR("cannot initiate bin buffer\n");
		return -1;
	}

	bin_push_int(clusterer_api.get_my_id());

	if (repl_prof_add(name, value?1:0, value, 0) < 0)
		return -1;
	dlg_replicate_profiles();
	return 0;
}
Exemple #10
0
int repl_prof_remove(str *name, str *value)
{
	bin_packet_t packet;

	if (profile_repl_cluster <= 0)
		return 0;
	if (bin_init(&packet, &prof_repl_cap, REPLICATION_DLG_PROFILE, BIN_VERSION, 1024) < 0) {
		LM_ERR("cannot initiate bin buffer\n");
		return -1;
	}

	if (repl_prof_add(&packet, name, value?1:0, value, 0) < 0)
		return -1;
	dlg_replicate_profiles(&packet);
	bin_free_packet(&packet);

	return 0;
}
Exemple #11
0
static void init_cpyprof(void)
{
    if (!Original_PyEval_EvalFrameEx) {
        Original_PyEval_EvalFrameEx = PyEval_EvalFrameEx;
        // monkey-patch PyEval_EvalFrameEx
        init_memprof_config_base();
        bin_init();
        create_tramp_table();
        size_t tramp_size;
        tramp_start = insert_tramp("PyEval_EvalFrameEx",
                                   &cpyprof_PyEval_EvalFrameEx,
                                   &tramp_size);
        tramp_end = tramp_start + tramp_size;
    }
    if (!Original_code_dealloc) {
        Original_code_dealloc = PyCode_Type.tp_dealloc;
        PyCode_Type.tp_dealloc = &cpyprof_code_dealloc;
    }
}
Exemple #12
0
/**
 * replicates a local dialog delete event to all the destinations
 * specified with the 'replicate_dialogs' modparam
 */
void replicate_dialog_deleted(struct dlg_cell *dlg)
{
	static str module_name = str_init("dialog");

	if (bin_init(&module_name, REPLICATION_DLG_DELETED, BIN_VERSION) != 0)
		goto error;

	bin_push_int(clusterer_api.get_my_id());
	bin_push_str(&dlg->callid);
	bin_push_str(&dlg->legs[DLG_CALLER_LEG].tag);
	bin_push_str(&dlg->legs[callee_idx(dlg)].tag);

	if (clusterer_api.send_to(dialog_replicate_cluster, PROTO_BIN) < 0) {
		goto error;
 	}

	return;
error:
	LM_ERR("Failed to replicate deleted dialog\n");
}
Exemple #13
0
/**
 * replicates a local dialog delete event to all the destinations
 * specified with the 'replicate_dialogs' modparam
 */
void replicate_dialog_deleted(struct dlg_cell *dlg)
{
	struct replication_dest *d;
	static str module_name = str_init("dialog");

	if (bin_init(&module_name, REPLICATION_DLG_DELETED) != 0)
		goto error;

	bin_push_str(&dlg->callid);
	bin_push_str(&dlg->legs[DLG_CALLER_LEG].tag);
	bin_push_str(&dlg->legs[callee_idx(dlg)].tag);

	for (d = replication_dests; d; d = d->next)
		bin_send(&d->to);

	if_update_stat(dlg_enable_stats,delete_sent,1);
	return;
error:
	LM_ERR("Failed to replicate deleted dialog\n");
}
Exemple #14
0
void replicate_ucontact_delete(urecord_t *r, ucontact_t *c)
{
	int rc;
	bin_packet_t packet;

	if (bin_init(&packet, &contact_repl_cap, REPL_UCONTACT_DELETE, BIN_VERSION, 0) != 0) {
		LM_ERR("failed to replicate this event\n");
		return;
	}

	bin_push_str(&packet, r->domain);
	bin_push_str(&packet, &r->aor);
	bin_push_str(&packet, &c->c);
	bin_push_str(&packet, &c->callid);
	bin_push_int(&packet, c->cseq);

	if (cluster_mode == CM_FEDERATION_CACHEDB)
		rc = clusterer_api.send_all_having(&packet, location_cluster,
		                                   NODE_CMP_EQ_SIP_ADDR);
	else
		rc = clusterer_api.send_all(&packet, location_cluster);
	switch (rc) {
	case CLUSTERER_CURR_DISABLED:
		LM_INFO("Current node is disabled in cluster: %d\n", location_cluster);
		goto error;
	case CLUSTERER_DEST_DOWN:
		LM_INFO("All destinations in cluster: %d are down or probing\n",
			location_cluster);
		goto error;
	case CLUSTERER_SEND_ERR:
		LM_ERR("Error sending in cluster: %d\n", location_cluster);
		goto error;
	}

	bin_free_packet(&packet);
	return;

error:
	LM_ERR("replicate ucontact delete failed\n");
	bin_free_packet(&packet);
}
/**
 * replicates a local dialog delete event to all the destinations
 * specified with the 'replicate_dialogs' modparam
 */
void replicate_dialog_deleted(struct dlg_cell *dlg)
{
	struct replication_dest *d;
	static str module_name = str_init("dialog");
	str send_buffer;

	if (bin_init(&module_name, REPLICATION_DLG_DELETED, BIN_VERSION) != 0)
		goto error;

	bin_push_str(&dlg->callid);
	bin_push_str(&dlg->legs[DLG_CALLER_LEG].tag);
	bin_push_str(&dlg->legs[callee_idx(dlg)].tag);

	bin_get_buffer(&send_buffer);

	for (d = replication_dests; d; d = d->next)
		msg_send(0,PROTO_BIN,&d->to,0,send_buffer.s,send_buffer.len,0);

	if_update_stat(dlg_enable_stats,delete_sent,1);
	return;
error:
	LM_ERR("Failed to replicate deleted dialog\n");
}
Exemple #16
0
/**
 * replicates a locally created dialog to all the destinations
 * specified with the 'replicate_dialogs' modparam
 */
void replicate_dialog_created(struct dlg_cell *dlg)
{
	static str module_name = str_init("dialog");
	int callee_leg;
	str *vars, *profiles;

	if (bin_init(&module_name, REPLICATION_DLG_CREATED, BIN_VERSION) != 0)
		goto error;

	bin_push_int(clusterer_api.get_my_id());

	callee_leg = callee_idx(dlg);

	bin_push_str(&dlg->callid);
	bin_push_str(&dlg->legs[DLG_CALLER_LEG].tag);
	bin_push_str(&dlg->legs[callee_leg].tag);

	bin_push_str(&dlg->from_uri);
	bin_push_str(&dlg->to_uri);

	bin_push_int(dlg->h_id);
	bin_push_int(dlg->start_ts);
	bin_push_int(dlg->state);

	bin_push_str(&dlg->legs[DLG_CALLER_LEG].bind_addr->sock_str);
	if (dlg->legs[callee_leg].bind_addr)
		bin_push_str(&dlg->legs[callee_leg].bind_addr->sock_str);
	else
		bin_push_str(NULL);

	bin_push_str(&dlg->legs[DLG_CALLER_LEG].r_cseq);
	bin_push_str(&dlg->legs[callee_leg].r_cseq);
	bin_push_str(&dlg->legs[DLG_CALLER_LEG].route_set);
	bin_push_str(&dlg->legs[callee_leg].route_set);
	bin_push_str(&dlg->legs[DLG_CALLER_LEG].contact);
	bin_push_str(&dlg->legs[callee_leg].contact);
	bin_push_str(&dlg->legs[callee_leg].from_uri);
	bin_push_str(&dlg->legs[callee_leg].to_uri);

	/* XXX: on shutdown only? */
	vars = write_dialog_vars(dlg->vals);
	dlg_lock_dlg(dlg);
	profiles = write_dialog_profiles(dlg->profile_links);
	dlg_unlock_dlg(dlg);

	bin_push_str(vars);
	bin_push_str(profiles);
	bin_push_int(dlg->user_flags);
	bin_push_int(dlg->flags &
			     ~(DLG_FLAG_NEW|DLG_FLAG_CHANGED|DLG_FLAG_VP_CHANGED));
	bin_push_int((unsigned int)time(0) + dlg->tl.timeout - get_ticks());
	bin_push_int(dlg->legs[DLG_CALLER_LEG].last_gen_cseq);
	bin_push_int(dlg->legs[callee_leg].last_gen_cseq);

	if (clusterer_api.send_to(dialog_replicate_cluster, PROTO_BIN) < 0)
 		goto error;

	if_update_stat(dlg_enable_stats,create_sent,1);
	return;

error:
	LM_ERR("Failed to replicate created dialog\n");
}
Exemple #17
0
EAPI_MAIN int
elm_main(int argc, char **argv)
{
	static prog_t handle;
	bin_t *bin = &handle.bin;

	handle.server_name = NULL;
	handle.session_id = NULL;
	handle.seq_size = SEQ_SIZE;

	bin->has_gui = true;

	fprintf(stderr,
		"Synthpod "SYNTHPOD_VERSION"\n"
		"Copyright (c) 2015 Hanspeter Portner ([email protected])\n"
		"Released under Artistic License 2.0 by Open Music Kontrollers\n");
	
	int c;
	while((c = getopt(argc, argv, "vhgGn:u:s:")) != -1)
	{
		switch(c)
		{
			case 'v':
				fprintf(stderr,
					"--------------------------------------------------------------------\n"
					"This is free software: you can redistribute it and/or modify\n"
					"it under the terms of the Artistic License 2.0 as published by\n"
					"The Perl Foundation.\n"
					"\n"
					"This source is distributed in the hope that it will be useful,\n"
					"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
					"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
					"Artistic License 2.0 for more details.\n"
					"\n"
					"You should have received a copy of the Artistic License 2.0\n"
					"along the source as a COPYING file. If not, obtain it from\n"
					"http://www.perlfoundation.org/artistic_license_2_0.\n\n");
				return 0;
			case 'h':
				fprintf(stderr,
					"--------------------------------------------------------------------\n"
					"USAGE\n"
					"   %s [OPTIONS] [BUNDLE_PATH]\n"
					"\n"
					"OPTIONS\n"
					"   [-v]                 print version and full license information\n"
					"   [-h]                 print usage information\n"
					"   [-g]                 enable GUI (default)\n"
					"   [-G]                 disable GUI\n"
					"   [-n] server-name     connect to named JACK daemon\n"
					"   [-u] client-uuid     client UUID for JACK session management\n"
					"   [-s] sequence-size   minimum sequence size (8192)\n\n"
					, argv[0]);
				return 0;
			case 'g':
				bin->has_gui = true;
				break;
			case 'G':
				bin->has_gui = false;
				break;
			case 'n':
				handle.server_name = optarg;
				break;
			case 'u':
				handle.session_id = optarg;
				break;
			case 's':
				handle.seq_size = MAX(SEQ_SIZE, atoi(optarg));
				break;
			case '?':
				if( (optopt == 'n') || (optopt == 'u') || (optopt == 's') )
					fprintf(stderr, "Option `-%c' requires an argument.\n", optopt);
				else if(isprint(optopt))
					fprintf(stderr, "Unknown option `-%c'.\n", optopt);
				else
					fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
				return -1;
			default:
				return -1;
		}
	}

	bin_init(bin);

	LV2_URID_Map *map = ext_urid_map_get(bin->ext_urid);

	lv2_atom_forge_init(&handle.forge, map);
	osc_forge_init(&handle.oforge, map);
	
	handle.midi_MidiEvent = map->map(map->handle, LV2_MIDI__MidiEvent);

	handle.time_position = map->map(map->handle, LV2_TIME__Position);
	handle.time_barBeat = map->map(map->handle, LV2_TIME__barBeat);
	handle.time_bar = map->map(map->handle, LV2_TIME__bar);
	handle.time_beatUnit = map->map(map->handle, LV2_TIME__beatUnit);
	handle.time_beatsPerBar = map->map(map->handle, LV2_TIME__beatsPerBar);
	handle.time_beatsPerMinute = map->map(map->handle, LV2_TIME__beatsPerMinute);
	handle.time_frame = map->map(map->handle, LV2_TIME__frame);
	handle.time_framesPerSecond = map->map(map->handle, LV2_TIME__framesPerSecond);
	handle.time_speed = map->map(map->handle, LV2_TIME__speed);

	bin->app_driver.system_port_add = _system_port_add;
	bin->app_driver.system_port_del = _system_port_del;

#if defined(JACK_HAS_CYCLE_TIMES)
	handle.osc_sched.osc2frames = _osc_schedule_osc2frames;
	handle.osc_sched.frames2osc = _osc_schedule_frames2osc;
	handle.osc_sched.handle = &handle;
	bin->app_driver.osc_sched = &handle.osc_sched;
#else
	bin->app_driver.osc_sched = NULL;
#endif
	bin->app_driver.features = SP_APP_FEATURE_POWER_OF_2_BLOCK_LENGTH; // always true for JACK

	bin->ui_driver.saved = _ui_saved;

	bin->ui_driver.features = SP_UI_FEATURE_NEW | SP_UI_FEATURE_SAVE | SP_UI_FEATURE_CLOSE;
	if(synthpod_nsm_managed() || handle.session_id)
		bin->ui_driver.features |= SP_UI_FEATURE_IMPORT_FROM | SP_UI_FEATURE_EXPORT_TO;
	else
		bin->ui_driver.features |= SP_UI_FEATURE_OPEN | SP_UI_FEATURE_SAVE_AS;

	// run
	bin_run(bin, argv, &nsm_driver);

	// stop
	bin_stop(bin);

	// deinit JACK
	_jack_deinit(&handle);

	// deinit
	bin_deinit(bin);

	return 0;
}
Exemple #18
0
int main(int argc, char *argv[]) 
{
  // Set directory structure
  directory_init(argc, argv);

  printf("Initializing...\n"); 
  fflush(stdout);
  // Read input file
  main_read_input();

  // Read and sort output directory for finding files within our time limits
  init_files();

  // Initialize domain and flow arrays
  domain_init(); 

  // Create output directory
  create_output();
  get_sigfigs();

  // Pull initial time step
  tt = 0;
  cgns_fill();

  // Find min and max and set up bins
  minmax(&min_vf, &max_vf, volume_fraction);
  minmax(&min_wp, &max_wp, part_w);
  minmax(&min_ke, &max_ke, part_ke);
  bin_init(min_vf, max_vf, &dBin_vf, &binStart_vf, &binEnd_vf);
  bin_init(min_wp, max_wp, &dBin_wp, &binStart_wp, &binEnd_wp);
  bin_init(min_ke, max_ke, &dBin_ke, &binStart_ke, &binEnd_ke);

  #ifdef DEBUG
    printf("  Bin Start (vf) = %lf\n", binStart_vf);
    printf("  Min (vf) = %lf\n", min_vf);
    printf("  dBin (vf) = %lf\n", dBin_vf);
    printf("  Max (vf) = %lf\n", max_vf);
    printf("  Bin End (vf) = %lf\n", binEnd_vf);
  #endif
    printf("  Bin Start (ke) = %lf\n", binStart_ke);
    printf("  Min (ke) = %lf\n", min_ke);
    printf("  dBin (ke) = %lf\n", dBin_ke);
    printf("  Max (ke) = %lf\n", max_ke);
    printf("  Bin End (ke) = %lf\n", binEnd_ke);

  // normalization for means
  double norm = 1./(dom.Gcc.s3 * nFiles);

  // Loop over time and bin
  printf("Looping...\n"); 
  fflush(stdout);
  int ind, ind_vf, ind_wp;

  for (tt = 0; tt < nFiles; tt++) {
    printf("  Timestep = %d of %d\n", tt+1, nFiles);
    fflush(stdout);
    // Fill data from cgns file
    cgns_fill();

    // Loop over space
    for (int cc = 0; cc < dom.Gcc.s3; cc++) {
      /* Volume Fraction */
      mean_vf += norm*volume_fraction[cc];
      // calculate index
      ind = (volume_fraction[cc] - binStart_vf)/dBin_vf;
      // if ind < 0, make it zero
      // if ind > nBins + 1, make it nBins +1
      ind = ind*(ind >= 0);         
      ind = ind*(ind <= (nBins + 1)) + (nBins + 1)*(ind > (nBins + 1));
      histogram_vf[ind]++;

      ind_vf = ind;

      /* Part Wp */
      mean_wp += norm*part_w[cc];
      ind = (part_w[cc] - binStart_wp)/dBin_wp;
      ind = ind*(ind >= 0);         
      ind = ind*(ind <= (nBins + 1)) + (nBins + 1)*(ind > (nBins + 1));
      histogram_wp[ind]++;

      ind_wp = ind;

      /* Bivariate */
      bihistogram_vf_wp[ind_wp + (nBins + 2)*ind_vf]++;

      /* Kinetic Energy */
      mean_ke += norm*part_ke[cc];
      ind = (part_ke[cc] - binStart_ke)/dBin_ke;
      ind = ind*(ind >= 0);         
      ind = ind*(ind <= (nBins + 1)) + (nBins + 1)*(ind > (nBins + 1));
      histogram_ke[ind]++;
    }

    // keep track of min max mean std for ALL files?
    // have bin padding as an input parameter?

  }
  // write to file
  write_field();

  // Free and exit
  printf("Done!\n");
  free_vars();
  return EXIT_SUCCESS;
}
Exemple #19
0
static VALUE
memprof_dump(int argc, VALUE *argv, VALUE self)
{
  VALUE str;
  FILE *out = NULL;

  if (!track_objs)
    rb_raise(rb_eRuntimeError, "object tracking disabled, call Memprof.start first");

  rb_scan_args(argc, argv, "01", &str);

  if (RTEST(str)) {
    out = fopen(StringValueCStr(str), "w");
    if (!out)
      rb_raise(rb_eArgError, "unable to open output file");
  }

  yajl_gen_config conf = { .beautify = 1, .indentString = "  " };
  yajl_gen gen = yajl_gen_alloc2((yajl_print_t)&json_print, &conf, NULL, (void*)out);

  track_objs = 0;

  yajl_gen_array_open(gen);
  st_foreach(objs, objs_each_dump, (st_data_t)gen);
  yajl_gen_array_close(gen);
  yajl_gen_free(gen);

  if (out)
    fclose(out);

  track_objs = 1;

  return Qnil;
}

static VALUE
memprof_dump_all(int argc, VALUE *argv, VALUE self)
{
  char *heaps = *(char**)bin_find_symbol("heaps",0);
  int heaps_used = *(int*)bin_find_symbol("heaps_used",0);

#ifndef sizeof__RVALUE
  size_t sizeof__RVALUE = bin_type_size("RVALUE");
#endif
#ifndef sizeof__heaps_slot
  size_t sizeof__heaps_slot = bin_type_size("heaps_slot");
#endif
#ifndef offset__heaps_slot__limit
  int offset__heaps_slot__limit = bin_type_member_offset("heaps_slot", "limit");
#endif
#ifndef offset__heaps_slot__slot
  int offset__heaps_slot__slot = bin_type_member_offset("heaps_slot", "slot");
#endif

  char *p, *pend;
  int i, limit;

  if (sizeof__RVALUE == 0 || sizeof__heaps_slot == 0)
    rb_raise(eUnsupported, "could not find internal heap");

  VALUE str;
  FILE *out = NULL;

  rb_scan_args(argc, argv, "01", &str);

  if (RTEST(str)) {
    out = fopen(StringValueCStr(str), "w");
    if (!out)
      rb_raise(rb_eArgError, "unable to open output file");
  }

  yajl_gen_config conf = { .beautify = 0, .indentString = "  " };
  yajl_gen gen = yajl_gen_alloc2((yajl_print_t)&json_print, &conf, NULL, (void*)out);

  track_objs = 0;

  //yajl_gen_array_open(gen);

  for (i=0; i < heaps_used; i++) {
    p = *(char**)(heaps + (i * sizeof__heaps_slot) + offset__heaps_slot__slot);
    limit = *(int*)(heaps + (i * sizeof__heaps_slot) + offset__heaps_slot__limit);
    pend = p + (sizeof__RVALUE * limit);

    while (p < pend) {
      if (RBASIC(p)->flags) {
        obj_dump((VALUE)p, gen);
        // XXX ugh
        yajl_gen_clear(gen);
        yajl_gen_free(gen);
        gen = yajl_gen_alloc2((yajl_print_t)&json_print, &conf, NULL, (void*)out);
        while(fputc('\n', out ? out : stdout) == EOF);
      }

      p += sizeof__RVALUE;
    }
  }

  //yajl_gen_array_close(gen);
  yajl_gen_clear(gen);
  yajl_gen_free(gen);

  if (out)
    fclose(out);

  track_objs = 1;

  return Qnil;
}

void
Init_memprof()
{
  VALUE memprof = rb_define_module("Memprof");
  eUnsupported = rb_define_class_under(memprof, "Unsupported", rb_eStandardError);
  rb_define_singleton_method(memprof, "start", memprof_start, 0);
  rb_define_singleton_method(memprof, "stop", memprof_stop, 0);
  rb_define_singleton_method(memprof, "stats", memprof_stats, -1);
  rb_define_singleton_method(memprof, "stats!", memprof_stats_bang, -1);
  rb_define_singleton_method(memprof, "track", memprof_track, -1);
  rb_define_singleton_method(memprof, "dump", memprof_dump, -1);
  rb_define_singleton_method(memprof, "dump_all", memprof_dump_all, -1);

  pagesize = getpagesize();
  objs = st_init_numtable();
  bin_init();
  create_tramp_table();

  gc_hook = Data_Wrap_Struct(rb_cObject, sourcefile_marker, NULL, NULL);
  rb_global_variable(&gc_hook);
  ptr_to_rb_mark_table_add_filename = bin_find_symbol("rb_mark_table_add_filename", NULL);

  rb_classname = bin_find_symbol("classname", 0);
  rb_add_freelist = bin_find_symbol("add_freelist", 0);

  insert_tramp("rb_newobj", newobj_tramp);
  insert_tramp("add_freelist", freelist_tramp);

  if (getenv("MEMPROF"))
    track_objs = 1;

  return;
}
/** test bin_find_entry function and bin_overflow_remove */
static void
test_bin_find_entry(struct lruhash* table)
{
	testkey_t* k = newkey(12);
	testdata_t* d = newdata(128);
	testkey_t* k2 = newkey(12 + 1024);
	testkey_t* k3 = newkey(14);
	testkey_t* k4 = newkey(12 + 1024*2);
	hashvalue_t h = myhash(12);
	struct lruhash_bin bin;
	memset(&bin, 0, sizeof(bin));
	bin_init(&bin, 1);

	/* remove from empty list */
	bin_overflow_remove(&bin, &k->entry);

	/* find in empty list */
	unit_assert( bin_find_entry(table, &bin, h, k) == NULL );

	/* insert */
	lock_quick_lock(&bin.lock);
	bin.overflow_list = &k->entry;
	lock_quick_unlock(&bin.lock);

	/* find, hash not OK. */
	unit_assert( bin_find_entry(table, &bin, myhash(13), k) == NULL );

	/* find, hash OK, but cmp not */
	unit_assert( k->entry.hash == k2->entry.hash );
	unit_assert( bin_find_entry(table, &bin, h, k2) == NULL );

	/* find, hash OK, and cmp too */
	unit_assert( bin_find_entry(table, &bin, h, k) == &k->entry );

	/* remove the element */
	lock_quick_lock(&bin.lock);
	bin_overflow_remove(&bin, &k->entry);
	lock_quick_unlock(&bin.lock);
	unit_assert( bin_find_entry(table, &bin, h, k) == NULL );

	/* prepend two different elements; so the list is long */
	/* one has the same hash, but different cmp */
	lock_quick_lock(&bin.lock);
	unit_assert( k->entry.hash == k4->entry.hash );
	k4->entry.overflow_next = &k->entry;
	k3->entry.overflow_next = &k4->entry;
	bin.overflow_list = &k3->entry;
	lock_quick_unlock(&bin.lock);

	/* find, hash not OK. */
	unit_assert( bin_find_entry(table, &bin, myhash(13), k) == NULL );

	/* find, hash OK, but cmp not */
	unit_assert( k->entry.hash == k2->entry.hash );
	unit_assert( bin_find_entry(table, &bin, h, k2) == NULL );

	/* find, hash OK, and cmp too */
	unit_assert( bin_find_entry(table, &bin, h, k) == &k->entry );

	/* remove middle element */
	unit_assert( bin_find_entry(table, &bin, k4->entry.hash, k4) 
		== &k4->entry );
	lock_quick_lock(&bin.lock);
	bin_overflow_remove(&bin, &k4->entry);
	lock_quick_unlock(&bin.lock);
	unit_assert( bin_find_entry(table, &bin, k4->entry.hash, k4) == NULL);

	/* remove last element */
	lock_quick_lock(&bin.lock);
	bin_overflow_remove(&bin, &k->entry);
	lock_quick_unlock(&bin.lock);
	unit_assert( bin_find_entry(table, &bin, h, k) == NULL );

	lock_quick_destroy(&bin.lock);
	delkey(k);
	delkey(k2);
	delkey(k3);
	delkey(k4);
	deldata(d);
}
Exemple #21
0
void replicate_ucontact_update(urecord_t *r, ucontact_t *ct)
{
	str st;
	int rc;
	bin_packet_t packet;

	if (bin_init(&packet, &contact_repl_cap, REPL_UCONTACT_UPDATE, BIN_VERSION, 0) != 0) {
		LM_ERR("failed to replicate this event\n");
		return;
	}

	bin_push_str(&packet, r->domain);
	bin_push_str(&packet, &r->aor);
	bin_push_str(&packet, &ct->c);
	bin_push_str(&packet, &ct->callid);
	bin_push_str(&packet, &ct->user_agent);
	bin_push_str(&packet, &ct->path);
	bin_push_str(&packet, &ct->attr);
	bin_push_str(&packet, &ct->received);
	bin_push_str(&packet, &ct->instance);

	st.s = (char *) &ct->expires;
	st.len = sizeof ct->expires;
	bin_push_str(&packet, &st);

	st.s = (char *) &ct->q;
	st.len = sizeof ct->q;
	bin_push_str(&packet, &st);

	bin_push_str(&packet, ct->sock?&ct->sock->sock_str:NULL);
	bin_push_int(&packet, ct->cseq);
	bin_push_int(&packet, ct->flags);
	bin_push_int(&packet, ct->cflags);
	bin_push_int(&packet, ct->methods);

	st.s   = (char *)&ct->last_modified;
	st.len = sizeof ct->last_modified;
	bin_push_str(&packet, &st);

	st = store_serialize(ct->kv_storage);
	if (ZSTR(st))
		LM_ERR("oom\n");
	bin_push_str(&packet, &st);
	store_free_buffer(&st);

	if (cluster_mode == CM_FEDERATION_CACHEDB)
		rc = clusterer_api.send_all_having(&packet, location_cluster,
		                                   NODE_CMP_EQ_SIP_ADDR);
	else
		rc = clusterer_api.send_all(&packet, location_cluster);
	switch (rc) {
	case CLUSTERER_CURR_DISABLED:
		LM_INFO("Current node is disabled in cluster: %d\n", location_cluster);
		goto error;
	case CLUSTERER_DEST_DOWN:
		LM_INFO("All destinations in cluster: %d are down or probing\n",
			location_cluster);
		goto error;
	case CLUSTERER_SEND_ERR:
		LM_ERR("Error sending in cluster: %d\n", location_cluster);
		goto error;
	}

	bin_free_packet(&packet);
	return;

error:
	LM_ERR("replicate ucontact update failed\n");
	bin_free_packet(&packet);
}
Exemple #22
0
void rl_timer_repl(utime_t ticks, void *param)
{
	unsigned int i = 0;
	map_iterator_t it;
	rl_pipe_t **pipe;
	str *key;
	int nr = 0;
	int ret;
	bin_packet_t packet;

	if (bin_init(&packet, &pipe_repl_cap, RL_PIPE_COUNTER, BIN_VERSION, 0) < 0) {
		LM_ERR("cannot initiate bin buffer\n");
		return;
	}

	/* iterate through each map */
	for (i = 0; i < rl_htable.size; i++) {
		RL_GET_LOCK(i);
		/* iterate through all the entries */
		if (map_first(rl_htable.maps[i], &it) < 0) {
			LM_ERR("map doesn't exist\n");
			goto next_map;
		}
		for (; iterator_is_valid(&it);) {
			pipe = (rl_pipe_t **) iterator_val(&it);
			if (!pipe || !*pipe) {
				LM_ERR("[BUG] bogus map[%d] state\n", i);
				goto next_pipe;
			}
			/* ignore cachedb replicated stuff */
			if (RL_USE_CDB(*pipe))
				goto next_pipe;

			key = iterator_key(&it);
			if (!key) {
				LM_ERR("cannot retrieve pipe key\n");
				goto next_pipe;
			}

			if (bin_push_str(&packet, key) < 0)
				goto error;

			if (bin_push_int(&packet, (*pipe)->algo) < 0)
				goto error;

			if (bin_push_int(&packet, (*pipe)->limit) < 0)
				goto error;

			/*
			 * for the SBT algorithm it is safe to replicate the current
			 * counter, since it is always updating according to the window
			 */
			if ((ret = bin_push_int(&packet,
						((*pipe)->algo == PIPE_ALGO_HISTORY ?
						 (*pipe)->counter : (*pipe)->my_last_counter))) < 0)
				goto error;
			nr++;

			if (ret > rl_buffer_th) {
				/* send the buffer */
				if (nr)
					rl_replicate(&packet);
				bin_reset_back_pointer(&packet);
				nr = 0;
			}

next_pipe:
			if (iterator_next(&it) < 0)
				break;
		}
next_map:
		RL_RELEASE_LOCK(i);
	}
	/* if there is anything else to send, do it now */
	if (nr)
		rl_replicate(&packet);
	bin_free_packet(&packet);
	return;
error:
	LM_ERR("cannot add pipe info in buffer\n");
	RL_RELEASE_LOCK(i);
	if (nr)
		rl_replicate(&packet);
	bin_free_packet(&packet);
}
static void repl_prof_utimer_f(utime_t ticks, void *param)
{

#define REPL_PROF_TRYSEND() \
	do { \
		nr++; \
		if (ret > repl_prof_buffer_th) { \
			/* send the buffer */ \
			if (nr) { \
				dlg_replicate_profiles(); \
				LM_DBG("sent %d records\n", nr); \
			} \
			if (bin_init(&module_name, REPLICATION_DLG_PROFILE, BIN_VERSION) < 0) { \
				LM_ERR("cannot initiate bin buffer\n"); \
				return; \
			} \
			nr = 0; \
		} \
	} while (0)

	struct dlg_profile_table *profile;
	static str module_name = str_init("dialog");
	map_iterator_t it;
	unsigned int count;
	int i;
	int nr = 0;
	int ret;
	void **dst;
	str *value;

	if (bin_init(&module_name, REPLICATION_DLG_PROFILE, BIN_VERSION) < 0) {
		LM_ERR("cannot initiate bin buffer\n");
		return;
	}

	for (profile = profiles; profile; profile = profile->next) {
		count = 0;
		if (!profile->has_value) {
			for (i = 0; i < profile->size; i++) {
				lock_set_get(profile->locks, i);
				count += profile->counts[i];
				lock_set_release(profile->locks, i);
			}

			if ((ret = repl_prof_add(&profile->name, 0, NULL, count)) < 0)
				goto error;
			/* check if the profile should be sent */
			REPL_PROF_TRYSEND();
		} else {
			for (i = 0; i < profile->size; i++) {
				lock_set_get(profile->locks, i);
				if (map_first(profile->entries[i], &it) < 0) {
					LM_ERR("map does not exist\n");
					goto next_entry;
				}
				while (iterator_is_valid(&it)) {
					dst = iterator_val(&it);
					if (!dst || !*dst) {
						LM_ERR("[BUG] bogus map[%d] state\n", i);
						goto next_val;
					}
					value = iterator_key(&it);
					if (!value) {
						LM_ERR("cannot retrieve profile's key\n");
						goto next_val;
					}
					count = repl_prof_get(dst);
					if ((ret = repl_prof_add(&profile->name, 1, value, count)) < 0)
						goto error;
					/* check if the profile should be sent */
					REPL_PROF_TRYSEND();

next_val:
					if (iterator_next(&it) < 0)
						break;
				}
next_entry:
				lock_set_release(profile->locks, i);
			}
		}
	}

	goto done;

error:
	LM_ERR("cannot add any more profiles in buffer\n");

done:
	/* check if there is anything else left to replicate */
	LM_DBG("sent %d records\n", nr);
	if (nr)
		dlg_replicate_profiles();
#undef REPL_PROF_TRYSEND
}
Exemple #24
0
static void broadcast_profiles(utime_t ticks, void *param)
{
#define REPL_PROF_TRYSEND() \
	do { \
		nr++; \
		if (ret > repl_prof_buffer_th) { \
			/* send the buffer */ \
			if (nr) \
				dlg_replicate_profiles(&packet); \
			bin_reset_back_pointer(&packet); \
			nr = 0; \
		} \
	} while (0)

	struct dlg_profile_table *profile;
	map_iterator_t it;
	unsigned int count;
	int i;
	int nr = 0;
	int ret;
	void **dst;
	str *value;
	bin_packet_t packet;

	if (bin_init(&packet, &prof_repl_cap, REPLICATION_DLG_PROFILE, BIN_VERSION, 0) < 0) {
		LM_ERR("cannot initiate bin buffer\n");
		return;
	}

	for (profile = profiles; profile; profile = profile->next) {
		if (!(profile->repl_type&REPL_PROTOBIN))
			continue;

		count = 0;
		if (!profile->has_value) {
			count = noval_get_local_count(profile);

			if ((ret = repl_prof_add(&packet, &profile->name, 0, NULL, count)) < 0)
				goto error;
			/* check if the profile should be sent */
			REPL_PROF_TRYSEND();
		} else {
			for (i = 0; i < profile->size; i++) {
				lock_set_get(profile->locks, i);
				if (map_first(profile->entries[i], &it) < 0) {
					LM_ERR("map does not exist\n");
					goto next_entry;
				}
				while (iterator_is_valid(&it)) {
					dst = iterator_val(&it);
					if (!dst || !*dst) {
						LM_ERR("[BUG] bogus map[%d] state\n", i);
						goto next_val;
					}
					value = iterator_key(&it);
					if (!value) {
						LM_ERR("cannot retrieve profile's key\n");
						goto next_val;
					}
					count = prof_val_get_local_count(dst);
					if ((ret = repl_prof_add(&packet, &profile->name, 1, value, count)) < 0)
						goto error;
					/* check if the profile should be sent */
					REPL_PROF_TRYSEND();

next_val:
					if (iterator_next(&it) < 0)
						break;
				}
next_entry:
				lock_set_release(profile->locks, i);
			}
		}
	}

	goto done;

error:
	LM_ERR("cannot add any more profiles in buffer\n");
	bin_free_packet(&packet);
done:
	/* check if there is anything else left to replicate */
	if (nr)
		dlg_replicate_profiles(&packet);
	bin_free_packet(&packet);
#undef REPL_PROF_TRYSEND
}
Exemple #25
0
void rl_timer_repl(utime_t ticks, void *param)
{
	static str module_name = str_init("ratelimit");
	unsigned int i = 0;
	map_iterator_t it;
	rl_pipe_t **pipe;
	str *key;
	int nr = 0;
	int ret;

	if (bin_init(&module_name, RL_PIPE_COUNTER) < 0) {
		LM_ERR("cannot initiate bin buffer\n");
		return;
	}

	/* iterate through each map */
	for (i = 0; i < rl_htable.size; i++) {
		RL_GET_LOCK(i);
		/* iterate through all the entries */
		if (map_first(rl_htable.maps[i], &it) < 0) {
			LM_ERR("map doesn't exist\n");
			goto next_map;
		}
		for (; iterator_is_valid(&it);) {
			pipe = (rl_pipe_t **)iterator_val(&it);
			if (!pipe || !*pipe) {
				LM_ERR("[BUG] bogus map[%d] state\n", i);
				goto next_pipe;
			}
			/* ignore cachedb replicated stuff */
			if (RL_USE_CDB(*pipe))
				goto next_pipe;

			key = iterator_key(&it);
			if (!key) {
				LM_ERR("cannot retrieve pipe key\n");
				goto next_pipe;
			}

			if (bin_push_str(key) < 0)
				goto error;

			if (bin_push_int((*pipe)->algo) < 0)
				goto error;

			if (bin_push_int((*pipe)->limit) < 0)
				goto error;

			if ((ret = bin_push_int((*pipe)->counter)) < 0)
				goto error;
			nr++;

			if (ret > rl_buffer_th) {
				/* send the buffer */
				if (nr)
					rl_replicate();
				if (bin_init(&module_name, RL_PIPE_COUNTER) < 0) {
					LM_ERR("cannot initiate bin buffer\n");
					RL_RELEASE_LOCK(i);
					return;
				}
				nr = 0;
			}

next_pipe:
			if (iterator_next(&it) < 0)
				break;
			}
next_map:
		RL_RELEASE_LOCK(i);
	}
	/* if there is anything else to send, do it now */
	if (nr)
		rl_replicate();
	return;
error:
	LM_ERR("cannot add pipe info in buffer\n");
	RL_RELEASE_LOCK(i);
	if (nr)
		rl_replicate();
}