コード例 #1
0
ファイル: kernel.c プロジェクト: ERJIALI/ee447
void
kernel()
{
	unsigned int then, now, delta;
	PUT32(0x40000054, INT_IRQ);		// mboxes 1..3 *do* interrupt, via IRQ
	PUT32(0x40000058, INT_IRQ);
	PUT32(0x4000005C, INT_IRQ);

	flash_lonum(3);

	then = now_usec();
	while (1) {
		now = now_usec();
        delta = usec_diff( now, then );
		//if (delta > ONE_MSEC) {
		if ( delta  > (ONE_SEC) ) {
		//if (delta > ((7 * ONE_SEC) + (500 * ONE_MSEC))) {
			then = now;
//			flash_lonum(3);
//			oldwait(20);
			interrupt_core(1);
//            clear_interrupt(1);
//			while (1) continue;
		}
	}
}
コード例 #2
0
ファイル: timer.c プロジェクト: gregfjohnson/Cloud-hub
/* debug-print the amount of time that has transpired since the box was
 * turned on in seconds, with fraction showing hundreth's of seconds.
 */
void ptime(char *title, struct timeval time)
{
    ddprintf("%s:  %.2f; ",
            title,
            (time.tv_sec == -1)
                ? -1
                : ((double) usec_diff(time.tv_sec, time.tv_usec,
                        start.tv_sec, start.tv_usec) / 1000000.));

}
コード例 #3
0
ファイル: frame_functions.c プロジェクト: tolmalev/SPM
void statistics (struct event_head * inbuf, struct statistics * st,
					 int n, int slot_size) {

	static struct irq_time last_time={0,0};
	struct event_head * point;
	double delta;
	int i;

	/* on the very first event clear all accumulators */

	if (last_time.sec == 0) {
		st->fifo_errors = 0;
		st->adc.min = 1e99;
		st->adc.avg = 0;
		st->adc.max = 0;
		st->service.min = 1e99;
		st->service.avg = 0;
		st->service.max = 0;
		st->interval.min = 1e99;
		st->interval.avg = 0;
		st->interval.max = 0;
		last_time = inbuf->irq_time;
		i = 1;
	} else {
		i = 0;
	}

	for ( ; i < n ; i++) {

		point = J_SLOT(inbuf,i);

		if (point->r_adc != point->n_adc * point->samples) {
			printf ("FIFO ERROR at point %d: %d\n",
				i, point->r_adc);
			st->fifo_errors += 1;
		}

		delta = point->adc_time;
		st->adc.avg += delta;
		if (delta > st->adc.max) st->adc.max = delta;
		if (delta < st->adc.min) st->adc.min = delta;

		delta = point->service_time;
		st->service.avg += delta;
		if (delta > st->service.max) st->service.max = delta;
		if (delta < st->service.min) st->service.min = delta;
		
		delta = usec_diff(&point->irq_time, &last_time);
		st->interval.avg += delta;
		if (delta > st->interval.max) st->interval.max = delta;
		if (delta < st->interval.min) st->interval.min = delta;

		last_time = point->irq_time;
	}
}
コード例 #4
0
ファイル: mac_list.c プロジェクト: gregfjohnson/Cloud-hub
/* find and delete any mac addresses that have not been refreshed since
 * the expiration time for the mac_list.
 * (time granularity is usecs.)
 */
int mac_list_expire_timed_macs(mac_list_t *mac_list)
{
    struct timeval tv;
    struct timezone tz;

    if (mac_list->timeout == -1) { return 0; }

    if (gettimeofday(&tv, &tz)) {
        fprintf(stderr, "expire_timed_macs:  gettimeofday failed\n");
        return -1;
    }

    while (1) {
        int i;
        int did_something = 0;
        for (i = 0; i < mac_list->next_beacon; i++) {
            if (usec_diff(tv.tv_sec, tv.tv_usec,
                mac_list->beacons[i].tv_sec, mac_list->beacons[i].tv_usec)
                        > mac_list->timeout)
            {
                int j;
                #ifdef DEBUG
                if (debug_file_inited) {
                    fprintf(debug_file, "%s; timeout ", mac_list->fname);
                    mac_print_no_eoln(debug_file,
                            mac_list->beacons[i].mac_addr);
                    fprintf(debug_file, " at ");
                    util_print_time(debug_file, &tv);
                    fprintf(debug_file, "\n");
                    fflush(debug_file);
                }
                #endif
                for (j = i; j < mac_list->next_beacon - 1; j++) {
                    mac_list->beacons[j] = mac_list->beacons[j + 1];
                }
                mac_list->next_beacon--;
                did_something = 1;
                break;
            }
        }
        if (!did_something) break;
    }

    return 0;
}
コード例 #5
0
ファイル: xbus-pcm.c プロジェクト: commshare/squeezeterisk
static int xpp_ticker_step(struct xpp_ticker *ticker, const struct timeval *t)
{
	unsigned long	flags;
	long		usec;
	bool		cycled = 0;

	spin_lock_irqsave(&ticker->lock, flags);
	ticker->last_sample.tv = *t;
	if((ticker->count % ticker->cycle) == ticker->cycle - 1) {	/* rate adjust */
		usec = (long)usec_diff(
				&ticker->last_sample.tv,
				&ticker->first_sample.tv);
		ticker->first_sample = ticker->last_sample;
		ticker->tick_period = usec / ticker->cycle;
		cycled = 1;
	}
	ticker->count++;
	spin_unlock_irqrestore(&ticker->lock, flags);
	return cycled;
}
コード例 #6
0
ファイル: cloud_box.c プロジェクト: konstellatio/Cloud-hub
/* print a table of the nodes in list using the print function.
 * use type-specific method to print each node.
 */
void node_list_print(ddprintf_t *fn, FILE *f, char *title,
                     node_t *list, int list_count)
{
    int i;
    struct timeval tv;
    long age;
    if (!checked_gettimeofday(&tv)) {
        memset(&tv, 0, sizeof(tv));
    }

    fn(f, "%s\n", title);

    for (i = 0; i < list_count; i++) {
        node_t *node = &list[i];
        age = usec_diff(tv.tv_sec, tv.tv_usec, node->sec, node->usec) / 1000;
        if (node->type == node_type_cloud_box) {
            cloud_box_print(fn, f, &node->box, 4);
        }
    }
}
コード例 #7
0
ファイル: xbus-pcm.c プロジェクト: commshare/squeezeterisk
static inline void pcm_frame_out(xbus_t *xbus, xframe_t *xframe)
{
	unsigned long	flags;
	struct timeval	now;
	unsigned long	usec;

	spin_lock_irqsave(&xbus->lock, flags);
	do_gettimeofday(&now);
	if(unlikely(disable_pcm || !TRANSPORT_RUNNING(xbus)))
		goto dropit;
	if(XPACKET_ADDR_SYNC((xpacket_t *)xframe->packets)) {
		usec = usec_diff(&now, &xbus->last_tx_sync);
		xbus->last_tx_sync = now;
		/* ignore startup statistics */
		if(likely(atomic_read(&xbus->pcm_rx_counter) > BIG_TICK_INTERVAL)) {
			if(abs(usec - 1000) > TICK_TOLERANCE) {
				static int	rate_limit;

				if((rate_limit++ % 5003) == 0)
					XBUS_DBG(SYNC, xbus, "Bad PCM TX timing(%d): usec=%ld.\n",
							rate_limit, usec);
			}
			if(usec > xbus->max_tx_sync)
				xbus->max_tx_sync = usec;
			if(usec < xbus->min_tx_sync)
				xbus->min_tx_sync = usec;
		}
	}
	spin_unlock_irqrestore(&xbus->lock, flags);
	/* OK, really send it */
	if(debug & DBG_PCM )
		dump_xframe("TX_XFRAME_PCM", xbus, xframe, debug);
	send_pcm_frame(xbus, xframe);
	XBUS_COUNTER(xbus, TX_XFRAME_PCM)++;
	return;
dropit:
	spin_unlock_irqrestore(&xbus->lock, flags);
	FREE_SEND_XFRAME(xbus, xframe);
}
コード例 #8
0
ファイル: timer.c プロジェクト: gregfjohnson/Cloud-hub
/* of the various timers (currently 8), figure out which one is due to
 * happen soonest, and set a timer interrupt to go off to wake us up then.
 */
void set_next_alarm()
{
    long long next_interrupt, maybe_next;

    while (!checked_gettimeofday(&now));

    if (db[2].d) {
        ptime("now", now);
        ptime("t0", times[0]);
        ptime("t1", times[1]);
        ptime("t2", times[2]);
        ptime("t3", times[3]);
        ptime("t4", times[4]);
        ptime("t5", times[5]);
        ptime("t6", times[6]);
        ptime("t7", times[7]);
        ddprintf("\n");
    }

    /* see if we are past time of next expected interrupt here, and if so
     * note it and reset timer for the future.
     */

    /* this is the exponential timer for stp_beacons */
    while ((next_interrupt = usec_diff(times[0].tv_sec, times[0].tv_usec,
            now.tv_sec, now.tv_usec)) < 0)
    {
        double wait_time;
        int iwait_time;
        if (db[39].d) {
            int mean_wait_time = MEAN_WAKEUP_TIME;
            if (stp_recv_beacon_count > 0) {
                mean_wait_time *= stp_recv_beacon_count;
            }
            wait_time = neg_exp(mean_wait_time);
        } else {
            wait_time = neg_exp(MEAN_WAKEUP_TIME);
        }
        iwait_time = (int) wait_time;
        if (db[40].d) {
            iwait_time *= 20;
            ddprintf("set stp_send timeout to %d\n", iwait_time);
        }
        usec_add_msecs(&times[0].tv_sec, &times[0].tv_usec, iwait_time);
        if (!db[54].d) {
            got_interrupt[send_stp] = 1;
        }
    }

    /* same as above */
    /* this is the wrt_beacon processing interval */
    while ((maybe_next = usec_diff(times[1].tv_sec, times[1].tv_usec,
            now.tv_sec, now.tv_usec)) < 0)
    {
        usec_add_msecs(&times[1].tv_sec, &times[1].tv_usec, intervals[1]);
        got_interrupt[process_beacon] = 1;
    }
    if (maybe_next < next_interrupt) { next_interrupt = maybe_next; }

    /* same as above */
    /* this is the eth_beacon processing interval */
    while ((maybe_next = usec_diff(times[2].tv_sec, times[2].tv_usec,
            now.tv_sec, now.tv_usec)) < 0)
    {
        usec_add_msecs(&times[2].tv_sec, &times[2].tv_usec, intervals[2]);
        got_interrupt[process_eth_beacon] = 1;
    }
    if (maybe_next < next_interrupt) { next_interrupt = maybe_next; }

    /* non-cloud message timeout */
    if (times[3].tv_sec != -1) {

        maybe_next = usec_diff(times[3].tv_sec, times[3].tv_usec,
                now.tv_sec, now.tv_usec);

        if (maybe_next < 0) {
            times[3].tv_sec = -1;
            got_interrupt[noncloud_message] = 1;

        } else if (maybe_next < next_interrupt) {
            next_interrupt = maybe_next;
        }
    }

    /* lockable resource timeout */
    if (times[4].tv_sec != -1) {

        maybe_next = usec_diff(times[4].tv_sec, times[4].tv_usec,
                now.tv_sec, now.tv_usec);

        if (maybe_next < 0) {
            times[4].tv_sec = -1;
            got_interrupt[lockable_timeout] = 1;
            if (db[28].d) {
                ddprintf("\nset_next_alarm; "
                        "detected lockable resource timeout..\n");
            }

        } else if (maybe_next < next_interrupt) {
            next_interrupt = maybe_next;
        }
    }

    /* print to cloud.asp timeout */
    if (times[5].tv_sec != -1) {

        maybe_next = usec_diff(times[5].tv_sec, times[5].tv_usec,
                now.tv_sec, now.tv_usec);

        if (maybe_next < 0) {
            times[5].tv_sec = -1;
            got_interrupt[print_cloud] = 1;
            if (db[28].d) {
                ddprintf("\nset_next_alarm; "
                        "detected print_cloud timeout..\n");
            }

        } else if (maybe_next < next_interrupt) {
            next_interrupt = maybe_next;
        }
    }

    /* ping neighbor timeout */
    if (times[6].tv_sec != -1) {

        maybe_next = usec_diff(times[6].tv_sec, times[6].tv_usec,
                now.tv_sec, now.tv_usec);

        if (maybe_next < 0) {
            times[6].tv_sec = -1;
            got_interrupt[ping_neighbors] = 1;
            if (db[28].d) {
                ddprintf("\nset_next_alarm; "
                        "detected ping_neighbors timeout..\n");
            }

        } else if (maybe_next < next_interrupt) {
            next_interrupt = maybe_next;
        }
    }

    /* disable cloud.asp printing timeout */
    if (times[7].tv_sec != -1) {

        maybe_next = usec_diff(times[7].tv_sec, times[7].tv_usec,
                now.tv_sec, now.tv_usec);

        if (maybe_next < 0) {
            times[7].tv_sec = -1;
            got_interrupt[disable_print_cloud] = 1;
            if (db[28].d) {
                ddprintf("\nset_next_alarm; "
                        "detected disable print cloud timeout..\n");
            }

        } else if (maybe_next < next_interrupt) {
            next_interrupt = maybe_next;
        }
    }

    /* wifi scan timeout */
    if (times[8].tv_sec != -1) {

        maybe_next = usec_diff(times[8].tv_sec, times[8].tv_usec,
                now.tv_sec, now.tv_usec);

        if (maybe_next < 0) {
            times[8].tv_sec = -1;
            got_interrupt[wifi_scan] = 1;
            if (db[28].d) {
                ddprintf("\nset_next_alarm; "
                        "detected wifi_scan timeout..\n");
            }

        } else if (maybe_next < next_interrupt) {
            next_interrupt = maybe_next;
        }
    }

    set_alarm((int) (next_interrupt / 1000));

    if (db[2].d) {
        int i;
        ptime("end", now);
        ptime("t0", times[0]);
        ptime("t1", times[1]);
        ptime("t2", times[2]);
        ptime("t3", times[3]);
        ptime("t4", times[4]);
        ptime("t5", times[5]);
        ptime("t6", times[6]);
        ptime("t7", times[7]);
        ptime("t8", times[8]);
        ddprintf("\ninterrupts pending: ");
        for (i = 0; i < TIMER_COUNT; i++) {
            if (got_interrupt[i]) {
                ddprintf(" %d", i);
            }
        }
        ddprintf("\nwaiting %d msec\n", (int) (next_interrupt / 1000));
    }
} /* set_next_alarm */
コード例 #9
0
ファイル: utils.cpp プロジェクト: Gleuwuzah/server-v4
timer::time_diff_t timer::usec_elapsed()const
{
    timespec now;
    clock_gettime(CLOCK_MONOTONIC, &now);
    return usec_diff(m_start, now);
}
コード例 #10
0
ファイル: mac_list.c プロジェクト: gregfjohnson/Cloud-hub
/* write the mac address list out to its specified file on disk,
 * including optional extra info with each mac address depending on 
 * mac_list's type.
 *
 * if the mac_list has write_delay == true , this routine is a
 * no-op if it's too soon since the last time we did the write.
 * see WRITE_DELAY above; max write frequency is currently once per second.
 */
int mac_list_write(mac_list_t *mac_list)
{
    struct timeval tv;
    struct timezone tz;
    char tmp_fname[PATH_MAX];
    FILE *f;
    int i;

    if (gettimeofday(&tv, &tz)) {
        fprintf(stderr, "mac_list_write:  gettimeofday failed\n");
        return -1;
    }

    if (mac_list->write_delay
        && usec_diff(tv.tv_sec, tv.tv_usec,
            mac_list->last_time_sec, mac_list->last_time_usec) < WRITE_DELAY)
    {
        return 0;
    }

    snprintf(tmp_fname, PATH_MAX, "/tmp/mac_list.%d", getpid());

    f = fopen(tmp_fname, "w");
    if (f == NULL) {
        fprintf(stderr, "mac_list_write:  fopen(\"%s\") failed\n", tmp_fname);
        return -1;
    }

    for (i = 0; i < mac_list->next_beacon; i++) {

        mac_print_no_eoln(f, mac_list->beacons[i].mac_addr);

        switch (mac_list->type) {

        case mac_list_beacon_name :
            fprintf(f, "    ");
            mac_print(f, mac_list->names[i]);

            break;

        case mac_list_beacon_signal_strength :
            fprintf(f, " %d\n", mac_list->signal_strength[i]);
            break;

        case mac_list_beacon :
            fprintf(f, "\n");
            break;

        case mac_list_beacon_desc :
            fprintf(f, "    %s", mac_list->desc[i]);
            if (strchr(mac_list->desc[i], '\n') == NULL) {
                fprintf(f, "\n");
            }

            break;

        default :
            fprintf(stderr, "mac_list_write:  invalid type %d\n",
                    mac_list->type);
            break;
        }
    }
    fclose(f);

    if (rename(tmp_fname, mac_list->fname)) {
        fprintf(stderr, "mac_list_write:  "
                "could not create %s:  %d\n", mac_list->fname, errno);
        perror("error:  ");
        unlink(tmp_fname);
        return -1;
    }

    mac_list->last_time_sec = tv.tv_sec;
    mac_list->last_time_usec = tv.tv_usec;

    return 0;
}
コード例 #11
0
ファイル: xbus-pcm.c プロジェクト: commshare/squeezeterisk
static void xpp_drift_step(xbus_t *xbus, const struct timeval *tv)
{
	struct xpp_drift	*driftinfo = &xbus->drift;
	struct xpp_ticker	*ticker = &xbus->ticker;
	unsigned long		flags;
	bool			cycled;

	spin_lock_irqsave(&driftinfo->lock, flags);
	cycled = xpp_ticker_step(&xbus->ticker, tv);
	if(ref_ticker && syncer && xbus->sync_mode == SYNC_MODE_PLL) {
		int	new_delta_tick = ticker->count - ref_ticker->count;
		int	lost_ticks = new_delta_tick - driftinfo->delta_tick;

		driftinfo->delta_tick = new_delta_tick;
		if(lost_ticks) {
			driftinfo->lost_ticks++;
			driftinfo->lost_tick_count += abs(lost_ticks);
			XBUS_DBG(SYNC, xbus, "Lost %d tick%s\n",
				lost_ticks,
				(abs(lost_ticks) > 1) ? "s": "");
			ticker->cycle = SYNC_ADJ_QUICK;
			if(abs(lost_ticks) > 100)
				ticker->count = ref_ticker->count;
		} else {
			long	usec_delta;
			bool	nofix = 0;

			usec_delta = (long)usec_diff(
					&ticker->last_sample.tv,
					&ref_ticker->last_sample.tv);
			usec_delta -= driftinfo->wanted_offset;
			sample_tick(xbus, usec_delta);
			if(abs(usec_delta) > 300) {
				/*
				 * We are close to the edge, send a brutal
				 * fix, and skip calculation until next time.
				 */
				if(usec_delta > 0 && xbus->sync_adjustment > -SYNC_ADJ_MAX) {
					XBUS_DBG(SYNC, xbus, "Pullback usec_delta=%ld\n", usec_delta);
					send_drift(xbus, -SYNC_ADJ_MAX);	/* emergency push */
				}
				if(usec_delta < 0 && xbus->sync_adjustment < SYNC_ADJ_MAX) {
					XBUS_DBG(SYNC, xbus, "Pushback usec_delta=%ld\n", usec_delta);
					send_drift(xbus, SYNC_ADJ_MAX);		/* emergency push */
				}
				ticker->cycle = SYNC_ADJ_QUICK;
				nofix = 1;
			} else {
				/* good data, use it */
				if(usec_delta > driftinfo->delta_max)
					driftinfo->delta_max = usec_delta;
				if(usec_delta < driftinfo->delta_min)
					driftinfo->delta_min = usec_delta;
			}
			if(!nofix && cycled) {
				int	offset = 0;

				driftinfo->median = (driftinfo->delta_max + driftinfo->delta_min) / 2;
				driftinfo->jitter = driftinfo->delta_max - driftinfo->delta_min;
				if(abs(driftinfo->median) >= 150) {	/* more than 1 usb uframe */
					int	factor = abs(driftinfo->median) / 125;

					factor = 1 + (factor * 8000) / ticker->cycle;
					if(driftinfo->median > 0)
						offset = driftinfo->calc_drift - factor;
					else
						offset = driftinfo->calc_drift + factor;
					/* for large median, push some more */
					if(abs(driftinfo->median) >= 300) {	/* more than 2 usb uframes */
						ticker->cycle = SYNC_ADJ_QUICK;
						XBUS_NOTICE(xbus,
								"Back to quick: median=%d\n",
								driftinfo->median);
					}
				} else {
					ticker->cycle += 500;
					if(ticker->cycle >= SYNC_ADJ_SLOW)
						ticker->cycle = SYNC_ADJ_SLOW;
				}
				driftinfo->calc_drift = offset;
				XBUS_DBG(SYNC, xbus,
						"ADJ: min=%d max=%d jitter=%d median=%d offset=%d\n",
						driftinfo->delta_min,
						driftinfo->delta_max,
						driftinfo->jitter,
						driftinfo->median,
						offset);
				if(offset < -SYNC_ADJ_MAX)
					offset = -SYNC_ADJ_MAX;
				if(offset > SYNC_ADJ_MAX)
					offset = SYNC_ADJ_MAX;
				xbus->sync_adjustment_offset = offset;
				if(xbus != syncer && xbus->sync_adjustment != offset)
					send_drift(xbus, offset);
				driftinfo_recalc(driftinfo);
			}
		}
	}
	spin_unlock_irqrestore(&driftinfo->lock, flags);
}
コード例 #12
0
ファイル: math_h.c プロジェクト: burnsba/codegolf
int main()
{
	double input;
	double s, c;
	
	char line [15];
	
	double * buffer;
	double * sin_buffer;
	double * cos_buffer;
	
	// do this in chunks of 1,000,000
	buffer = (double*) malloc (sizeof(double) * CHUNK_READ_COUNT);
	
	sin_buffer = (double*) malloc (sizeof(double) * CHUNK_READ_COUNT);
	cos_buffer = (double*) malloc (sizeof(double) * CHUNK_READ_COUNT);
	
	FILE *ifp = fopen("trig.in", "r");
	FILE *ofp = fopen("trig.out", "w");
	
	int do_stuff = 1;
	int last_read_count = 0;
	int i;
	
	struct timeval start_time;
	gettimeofday(&start_time,NULL);
	
	uint64_t usec_math_count = 0;
	uint64_t usec_total = 0;
	
	while (1)
	{
		memset (buffer, 0, sizeof(double) * CHUNK_READ_COUNT);
		memset (sin_buffer, 0, sizeof(double) * CHUNK_READ_COUNT);
		memset (cos_buffer, 0, sizeof(double) * CHUNK_READ_COUNT);
		
		last_read_count = 0;
		
		// read inputs to buffer
		for (i=0; i<CHUNK_READ_COUNT; i++)
		{
			do_stuff = fgets (line, 15, ifp) != (char*)0;
			if (!do_stuff)
			{
				break;
			}

			buffer[i] = atof(line);
		}
		
		last_read_count = i;
		
		// do math
		struct timeval math_time;
		gettimeofday(&math_time,NULL);
		
		for (i=0; i<last_read_count; i++)
		{
			//sin_buffer[i] = minimaxsin(buffer[i] * PI_OVER_180);
			//cos_buffer[i] = minimaxsin(PI_OVER_2 - buffer[i] * PI_OVER_180);
			sin_buffer[i] = sin(buffer[i] * PI_OVER_180);
			cos_buffer[i] = sin(PI_OVER_2 - buffer[i] * PI_OVER_180);
		}
		
		usec_math_count += usec_diff(math_time);
		
		// save latest output batch
		for (i=0; i<last_read_count; i++)
		{
			if (cos_buffer[i] < 0.000000001 && cos_buffer[i] > -0.000000001)
			{
				fprintf(ofp, "%.7f %.7f n\n", sin_buffer[i], cos_buffer[i]);
			}
			else
			{
				fprintf(ofp, "%.7f %.7f %.7f\n", sin_buffer[i], cos_buffer[i], (sin_buffer[i]/cos_buffer[i]));
			}
		}
		
		if (!do_stuff)
		{
			break;
		}
	}

	fclose(ifp);
	fclose(ofp);
	
	usec_total += usec_diff(start_time);
	
	printf("estimated math time in usec: %llu\n", usec_math_count);
	printf("estimated run time in usec: %llu\n", usec_total);

	return 0;
}