示例#1
0
int main(int argc, char *argv[]) {

	struct timespec start, end;
	int sz = 0, *data = NULL;

	switch (argc) {
		case 2:
			printf("Generating %d numbers\n", sz = atoi(argv[1]));
			clock_gettime(CLOCK_MONOTONIC, &start);
			data = alloc_random_data(sz);
			print_data("allocated", data, sz);
			clock_gettime(CLOCK_MONOTONIC, &end);
			printf("Generating took %ld usecs\n", timespec_diff_us(&start, &end));
			break;
		default:
			printf("Bad input arguments!\n");
			exit(-1);
	}

	printf("Computing median...\n");
	clock_gettime(CLOCK_MONOTONIC, &start);
	int median = find_median(data, 0, sz-1, sz);
	clock_gettime(CLOCK_MONOTONIC, &end);

	free(data);

	printf("Median = %d\n", median);
	printf("Running time = %ld usecs\n", timespec_diff_us(&start, &end));

	return 0;
}
static int amd_fam14h_stop(void)
{
	int num, cpu;
	struct timespec end_time;

	clock_gettime(CLOCK_REALTIME, &end_time);

	for (num = 0; num < AMD_FAM14H_STATE_NUM; num++) {
		for (cpu = 0; cpu < cpu_count; cpu++)
			amd_fam14h_disable(&amd_fam14h_cstates[num], cpu);
	}
#ifdef DEBUG
	clock_gettime(CLOCK_REALTIME, &dbg_time);
	dbg_timediff = timespec_diff_us(end_time, dbg_time);
	dprint("Disabling counters took: %lu ns\n", dbg_timediff);
#endif
	timediff = timespec_diff_us(start_time, end_time);
	if (timediff / 1000 > OVERFLOW_MS)
		print_overflow_err((unsigned int)timediff / 1000000,
				   OVERFLOW_MS / 1000);

	return 0;
}
static int amd_fam14h_start(void)
{
	int num, cpu;
	clock_gettime(CLOCK_REALTIME, &start_time);
	for (num = 0; num < AMD_FAM14H_STATE_NUM; num++) {
		for (cpu = 0; cpu < cpu_count; cpu++)
			amd_fam14h_init(&amd_fam14h_cstates[num], cpu);
	}
#ifdef DEBUG
	clock_gettime(CLOCK_REALTIME, &dbg_time);
	dbg_timediff = timespec_diff_us(start_time, dbg_time);
	dprint("Enabling counters took: %lu us\n",
	       dbg_timediff);
#endif
	return 0;
}
static int cpuidle_stop(void)
{
	int cpu, state;
	struct timespec end_time;
	clock_gettime(CLOCK_REALTIME, &end_time);
	timediff = timespec_diff_us(start_time, end_time);

	for (cpu = 0; cpu < cpu_count; cpu++) {
		for (state = 0; state < cpuidle_sysfs_monitor.hw_states_num;
		     state++) {
			current_count[cpu][state] =
				sysfs_get_idlestate_time(cpu, state);
			dprint("CPU %d - State: %d - Val: %llu\n",
			       cpu, state, previous_count[cpu][state]);
		}
	};
	return 0;
}
示例#5
0
static bool write_data(struct a2dp_stream_out *out, const void *buffer,
								size_t bytes)
{
	struct audio_endpoint *ep = out->ep;
	struct media_packet *mp = (struct media_packet *) ep->mp;
	struct media_packet_rtp *mp_rtp = (struct media_packet_rtp *) ep->mp;
	size_t free_space = ep->mp_data_len;
	size_t consumed = 0;

	while (consumed < bytes) {
		size_t written = 0;
		ssize_t read;
		uint32_t samples;
		int ret;
		struct timespec current;
		uint64_t audio_sent, audio_passed;
		bool do_write = false;

		/*
		 * prepare media packet in advance so we don't waste time after
		 * wakeup
		 */
		if (ep->codec->use_rtp) {
			mp_rtp->hdr.sequence_number = htons(ep->seq++);
			mp_rtp->hdr.timestamp = htonl(ep->samples);
		}
		read = ep->codec->encode_mediapacket(ep->codec_data,
						buffer + consumed,
						bytes - consumed, mp,
						free_space, &written);

		/*
		 * not much we can do here, let's just ignore remaining
		 * data and continue
		 */
		if (read <= 0)
			return true;

		/* calculate where are we and where we should be */
		clock_gettime(CLOCK_MONOTONIC, &current);
		if (!ep->samples)
			memcpy(&ep->start, &current, sizeof(ep->start));
		audio_sent = ep->samples * 1000000ll / out->cfg.rate;
		audio_passed = timespec_diff_us(&current, &ep->start);

		/*
		 * if we're ahead of stream then wait for next write point,
		 * if we're lagging more than 100ms then stop writing and just
		 * skip data until we're back in sync
		 */
		if (audio_sent > audio_passed) {
			struct timespec anchor;

			ep->resync = false;

			timespec_add(&ep->start, audio_sent, &anchor);

			while (true) {
				ret = clock_nanosleep(CLOCK_MONOTONIC,
							TIMER_ABSTIME, &anchor,
							NULL);

				if (!ret)
					break;

				if (ret != EINTR) {
					error("clock_nanosleep failed (%d)",
									ret);
					return false;
				}
			}
		} else if (!ep->resync) {
			uint64_t diff = audio_passed - audio_sent;

			if (diff > MAX_DELAY) {
				warn("lag is %jums, resyncing", diff / 1000);

				ep->codec->update_qos(ep->codec_data,
							QOS_POLICY_DECREASE);
				ep->resync = true;
			}
		}

		/* we send data only in case codec encoded some data, i.e. some
		 * codecs do internal buffering and output data only if full
		 * frame can be encoded
		 * in resync mode we'll just drop mediapackets
		 */
		if (written > 0 && !ep->resync) {
			/* wait some time for socket to be ready for write,
			 * but we'll just skip writing data if timeout occurs
			 */
			if (!wait_for_endpoint(ep, &do_write))
				return false;

			if (do_write) {
				if (ep->codec->use_rtp)
					written += sizeof(struct rtp_header);

				if (!write_to_endpoint(ep, written))
					return false;
			}
		}

		/*
		 * AudioFlinger provides 16bit PCM, so sample size is 2 bytes
		 * multiplied by number of channels. Number of channels is
		 * simply number of bits set in channels mask.
		 */
		samples = read / (2 * popcount(out->cfg.channels));
		ep->samples += samples;
		consumed += read;
	}

	return true;
}