Ejemplo n.º 1
0
int
main(void)
{
        struct items {
                char *up;
                char *load;
                char *fan;
                char *temp;
                char *mem;
                char *swap;
                char *diskio;
                char *fs_root;
                char *fs_home;
                char *fs_storage;
                char *net_speed;
                char *batt;
                char *vol;
                char *time;
                char *status;
        };

	if (!(dpy = XOpenDisplay(NULL))) {
		fprintf(stderr, "dwmstatus: cannot open display.\n");
		return 1;
    }

        struct items *i = malloc(sizeof(struct items));

	while(1) {
		i->up = get_up();
		i->load = get_load();
		i->fan = get_fan();
		i->temp = get_temp();
		i->mem = get_mem();
		i->swap = get_swap();
		i->diskio = get_diskio("sda");
                i->fs_root = get_space("/");
                i->fs_home = get_space("/home");
                i->net_speed = get_net_speed();
		i->batt = get_batt();
		i->vol = get_vol();
		i->time = mktimes("%m/%d/%Y %a %H:%M", tzkiev);

		i->status = smprintf(
                        "up:%s la:%s fan:%s temp:%s m:%s s:%s io:%s /:%s "
                        "~/:%s net:%s bat:%s vol:%s %s",
                        i->up, i->load, i->fan, i->temp, i->mem, i->swap,
                        i->diskio, i->fs_root, i->fs_home, i->net_speed,
                        i->batt, i->vol, i->time);
		setstatus(i->status);

                sleep(INTERVAL);
    }

        free(i);

	XCloseDisplay(dpy);

	return EXIT_SUCCESS;
}
Ejemplo n.º 2
0
struct Node* connected_double_edge_swap(struct Node* G, int node_count, int nswap, int windows_threhold){
    // this version implement the windows size.
    srand((unsigned int)time(NULL));
    int i;
    struct Node* node1;
    struct Node* node2;
    struct Node* n1_rel;
    struct Node* n2_rel;
    int max_id = 0;
    // copy G
    // directly copy may be dingerous
    for (int i = 0; i < node_count; i++) {
        struct Node * n = malloc(sizeof(struct Node));
        n->node_id = G[i].node_id;
        if (n->node_id > max_id) {
            max_id = n->node_id;
        }
        n->neighbour_count = G[i].neighbour_count;
        n->neighbours = (int *)malloc(sizeof(int) * n->neighbour_count);
        for (int j = 0; j < n->neighbour_count; j ++) {
            n->neighbours[j] = G[i].neighbours[j];
        }
        G[i] = *n;
    }
    struct Node ** id2Node = malloc(sizeof(struct Node) * (max_id + 1));
    for (int i = 0; i < max_id + 1; i ++) {
        id2Node[i] = NULL;
    }
    for (int i = 0; i < node_count; i ++) {
        id2Node[G[i].node_id] = G + i;
    }
    i = 0;
    struct CDF * cdf = generate_cdf(G, node_count);
    windows_threhold = 3;
    int windows = 1;
//    printf("\nStArT\n");
    while (i < nswap) {
        int wcount = 0;
        init_heap();
        if (windows < windows_threhold) {
            // do check every time
//            printf("low windows count\n");
            int fail = 0;
            while (wcount < windows && i < nswap) {
//                if (i % 100000 == 0) {
//                    printf("done: %i\n", i);
//                }
//                printf("Low: i: %i, wcount: %i, windows: %i\n", i, wcount, windows);
                int * picked = choose_node_from_cdf(cdf, (float)(rand()) / (float)(RAND_MAX), (float)(rand()) / (float)(RAND_MAX));
                int n1 = picked[0];
                int n2 = picked[1];
                if (n1 == n2) {
                    continue;
                }
                // the node may be empty?
                node1 = get_node_from_graph(G, node_count, n1);
                node2 = get_node_from_graph(G, node_count, n2);
                if (node1 == NULL || node2 == NULL) {
                    printf("Oops node empty %i, %i\n", n1, n2);
                    continue;
                }
                int ri, rj;
                int n_id_1 = pick_neighbour(node1, &ri);
                int n_id_2 = pick_neighbour(node2, &rj);
                n1_rel = get_node_from_graph(G, node_count, n_id_1);
                n2_rel = get_node_from_graph(G, node_count, n_id_2);
                // any NULL error and continue
                if (node1 == NULL || node2 == NULL || n1_rel == NULL || n2_rel == NULL) {
                    // printf("node1 %llx, node2 %llx, n1_rel %llx n2_rel %llx\n", node1, node2, n1_rel, n2_rel);
                    continue;
                }
                if (node1->node_id == n2_rel->node_id || node2->node_id == n1_rel->node_id || n1_rel->node_id == n2_rel->node_id) {
                    continue;
                }
                if (is_two_node_connected(node1, node2) || is_two_node_connected(n1_rel, n2_rel)) {
                    continue;
                }
                // now let us do the exchage
                // before:   node1 -- n1_rel        after:   node1   n1_rel
                //                                             |       |
                //           node2 -- n2_rel                 node2   n2_rel
                // delete edge
                delete_edge(node1, n1_rel);
                delete_edge(node2, n2_rel);
                // add edge
                add_edge(node1, node2);
                add_edge(n1_rel, n2_rel);
                i += 1;
                add_swap(node1->node_id, node2->node_id, n1_rel->node_id, n2_rel->node_id);
                //        if (! is_graph_connected(G, node_count, id2Node, max_id)) {
                if (! is_2_node_connected(id2Node, max_id, node1->node_id, n1_rel->node_id)) {
                    // oops let us redo
//                    printf("Oops not connected, retry~\n");
                    add_edge(node1, n1_rel);
                    add_edge(node2, n2_rel);
                    // add edge
                    delete_edge(node1, node2);
                    delete_edge(n1_rel, n2_rel);
                    fail = 1;
                } else{
                    wcount += 1;
                }
            }
            if (fail == 1){
                windows = ceilf((float)windows / 2);
            }else{
                windows += 1;
            }
        }else{
//            printf("high windows count\n");
            // do check at end
            while (wcount < windows && i < nswap) {
//                if (i % 100000 == 0) {
//                    printf("done: %i\n", i);
//                }
//                printf("High: i: %i, wcount: %i, window: %i\n", i, wcount, windows);
                int * picked = choose_node_from_cdf(cdf, (float)(rand()) / (float)(RAND_MAX), (float)(rand()) / (float)(RAND_MAX));
                int n1 = picked[0];
                int n2 = picked[1];
                if (n1 == n2) {
                    continue;
                }
                // the node may be empty?
                node1 = get_node_from_graph(G, node_count, n1);
                node2 = get_node_from_graph(G, node_count, n2);
                if (node1 == NULL || node2 == NULL) {
//                    printf("Oops node empty %i, %i\n", n1, n2);
                    continue;
                }
                int ri, rj;
                int n_id_1 = pick_neighbour(node1, &ri);
                int n_id_2 = pick_neighbour(node2, &rj);
                n1_rel = get_node_from_graph(G, node_count, n_id_1);
                n2_rel = get_node_from_graph(G, node_count, n_id_2);
                // any NULL error and continue
                if (node1 == NULL || node2 == NULL || n1_rel == NULL || n2_rel == NULL) {
                    // printf("node1 %llx, node2 %llx, n1_rel %llx n2_rel %llx\n", node1, node2, n1_rel, n2_rel);
                    continue;
                }
                if (node1->node_id == n2_rel->node_id || node2->node_id == n1_rel->node_id || n1_rel->node_id == n2_rel->node_id) {
                    continue;
                }
                if (is_two_node_connected(node1, node2) || is_two_node_connected(n1_rel, n2_rel)) {
                    continue;
                }
                // now let us do the exchage
                // before:   node1 -- n1_rel        after:   node1   n1_rel
                //                                             |       |
                //           node2 -- n2_rel                 node2   n2_rel
                // delete edge
                delete_edge(node1, n1_rel);
                delete_edge(node2, n2_rel);
                // add edge
                add_edge(node1, node2);
                add_edge(n1_rel, n2_rel);
                i += 1;
                add_swap(node1->node_id, node2->node_id, n1_rel->node_id, n2_rel->node_id);
                wcount += 1;
            }
            if (is_graph_connected(G, node_count, id2Node, max_id)) {
                windows += 1;
            }else{
                // then redo all
//                printf("Oops not connected, retry~\n");
                while (1) {
//                    printf("redo\n");
                    struct Swap* s = get_swap();
                    if (s == NULL) {
                        break;
                    }
                    struct Node * reN1 = get_node_from_graph(G, node_count, s->node1);
                    struct Node * reN2 = get_node_from_graph(G, node_count, s->node2);
                    struct Node * reN1_rel = get_node_from_graph(G, node_count, s->node3);
                    struct Node * reN2_rel = get_node_from_graph(G, node_count, s->node4);
                    add_edge(reN1, reN1_rel);
                    add_edge(reN2, reN2_rel);
                    // add edge
                    delete_edge(reN1, reN2);
                    delete_edge(reN1_rel, reN2_rel);
                }
                windows = ceilf((float)windows / 2.0);
            }
        }
    }
    return G;
}
Ejemplo n.º 3
0
int calculate(int veid, ub_param *ub, int verbose)
{
	ub_param ub_cur;
	char tmp[STR_SIZE];
	int ret = 0;
	double kmem_net_cur, lm_cur, tr_cur, ms_cur, al_cur, np_cur;
	double kmem_net_max, lm_max, lm_prm, ms_prm, al_prm, al_max, np_max;
	double cur, prm, max;
	unsigned long long lowmem;
	unsigned long long ram, swap = 1;
	int run, thrmax;
	int page;

	if (check_param(ub))
		return 1;
	if (get_mem(&ram))
		return 1;
	if (get_thrmax(&thrmax))
		return 1;
	get_swap(&swap);
	if (get_lowmem(&lowmem))
		return 1;
	page = get_pagesize();
	lm_cur = tr_cur = ms_cur = al_cur = np_cur = 0;
	lowmem *= 0.4;
	memset(&ub_cur, 0, sizeof(ub_cur));
	if (!(run = vps_read_ubc(veid, &ub_cur))) {
		if (check_param(&ub_cur))
			return 1;
		kmem_net_cur = (double)ub_cur.kmemsize[0] +
			(double)ub_cur.tcprcvbuf[0] +
			(double)ub_cur.tcpsndbuf[0] +
			(double)ub_cur.dgramrcvbuf[0] +
			(double)ub_cur.othersockbuf[0];
		/*	Low memory	*/
		lm_cur = kmem_net_cur / lowmem;
		/*	Total RAM	*/
		tr_cur = ((double)ub_cur.physpages[0] * page + kmem_net_cur)
					/ ram;
		/*	Mem + Swap	*/
		ms_cur = ((double)ub_cur.oomguarpages[0] * page +
				kmem_net_cur) /	(ram + swap);
		/*	Allocated	*/
		al_cur = ((double)ub_cur.privvmpages[0] * page +
				kmem_net_cur) / (ram + swap);
		/*	Numproc		*/
		np_cur = (double)ub_cur.numproc[0] / thrmax;
	}
	kmem_net_max = (double)ub->kmemsize[1] +
		(double)ub->tcprcvbuf[1] +
		(double)ub->tcpsndbuf[1] +
		(double)ub->dgramrcvbuf[1] +
		(double)ub->othersockbuf[1];

	/*	Low memory	*/
	lm_max = kmem_net_max / lowmem;
	lm_prm = lm_max;
	/*	Mem + Swap	*/
	ms_prm = ((double)ub->oomguarpages[0] * page + kmem_net_max) /
			(ram + swap);
	/*	Allocated	*/
	al_prm = ((double)ub->vmguarpages[0] * page + kmem_net_max) /
			(ram + swap);
	al_max = ((double)ub->privvmpages[1] * page + kmem_net_max) /
			(ram + swap);
	/*	Numproc		*/
	np_max = (double)ub->numproc[0] / thrmax;

	/* Calculate maximum for current */
	cur = lm_cur;
	if (tr_cur > cur)
		cur = tr_cur;
	if (ms_cur > cur)
		cur = ms_cur;
	if (al_cur > cur)
		cur = al_cur;
	if (np_cur > cur)
		cur = np_cur;
	/* Calculate maximum for promised */
	prm = lm_prm;
	if (ms_prm > prm)
		prm = ms_prm;
	if (al_prm > prm)
		prm = al_prm;
	/* Calculate maximum for Max */
	max = lm_max;
	if (al_max > max)
		max = al_max;
	if (np_max > max)
		max = np_max;
	sprintf(tmp, "n/a");
	printf("Resource     Current(%%)  Promised(%%)  Max(%%)\n");
	if (verbose) {
		if (!run)
			sprintf(tmp, "%10.2f", lm_cur * 100);
		printf("Low Mem    %10s %10.2f %10.2f\n",
				tmp, lm_prm * 100, lm_max * 100);
		if (!run)
			sprintf(tmp, "%10.2f", tr_cur * 100);
		printf("Total RAM  %10s        n/a        n/a \n", tmp);
		if (!run)
			sprintf(tmp, "%10.2f", ms_cur * 100);
		printf("Mem + Swap %10s %10.2f        n/a\n",
				tmp, ms_prm * 100);
		if (!run)
			sprintf(tmp, "%10.2f", al_cur * 100);
		printf("Alloc. Mem %10s %10.2f %10.2f\n",
				tmp , al_prm * 100, al_max * 100);
		if (!run)
			sprintf(tmp, "%10.2f", np_cur * 100);
		printf("Num. Proc  %10s        n/a %10.2f\n",
				tmp, np_max * 100);
		printf("--------------------------------------------\n");
	}
	if (!run)
		sprintf(tmp, "%10.2f", cur * 100);
	printf("Memory     %10s %10.2f %10.2f\n", tmp, prm * 100, max * 100);
	free_ub_param(&ub_cur);
	return ret;
}