Project

General

Profile

Bug #283 » minimal.c

minimal.c program - Raphaël Beamonte, 06/20/2012 04:12 PM

 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <sched.h>
#include <sys/mman.h>
#include <errno.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <ctype.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/param.h>
#include <sys/io.h>

#define TRACEPOINT_DEFINE
#include "ust_xhp.h"

#define TRACE_SESSION_NAME "xhp"
#include <lttng/lttng.h>

//#define TRACE_KERNEL
#define TRACE_UST

int main(int argc, char*argv[])
{

/*volatile int w = 0;
while (w < 1) {
w++;
}*/

// LTTng variables
int ret = 0;

// Running as root ?
if (getuid() != 0) {
printf("Root access is needed. -- Aborting!\n");
return EXIT_SUCCESS;
}

/* starting LTTng */

// Generation of the logdir name
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);

char path[MAXPATHLEN];
getcwd(path, MAXPATHLEN);

int sizelogdir = strlen(path)+1+strlen(TRACE_SESSION_NAME)+16+1;
char logdir[sizelogdir], date[16];

strftime(date, 16, "%Y%m%d-%H%M%S", timeinfo);
snprintf(logdir, sizelogdir, "%s/%s-%s", path, TRACE_SESSION_NAME, date);

//// Creating LTTng session
if ((ret = lttng_create_session(TRACE_SESSION_NAME, logdir)) < 0) {
printf("LTTng: error creating the session : %s\n", lttng_strerror(ret));
return 1;
} else {
printf("LTTng: traces will be saved at %s\n\n", logdir);
}

#ifdef TRACE_KERNEL
struct lttng_domain domain_kernel = {0};
struct lttng_event event_kernel = {0};
struct lttng_handle *handle_kernel;

//// Adding kernel trace
// Creating the handle
domain_kernel.type = LTTNG_DOMAIN_KERNEL;
if ((handle_kernel = lttng_create_handle(TRACE_SESSION_NAME, &domain_kernel)) == NULL) {
printf("LTTng: error creating handle: %s\n", lttng_strerror(ret));
return 2;
}

// Enabling the event
memset(&event_kernel, 0, sizeof(struct lttng_event));
event_kernel.type = LTTNG_EVENT_ALL;

if ((ret = lttng_enable_event(handle_kernel, &event_kernel, NULL)) < 0) {
printf("LTTng: error enabling event: %s\n", lttng_strerror(ret));
return 3;
}
#endif /* TRACE_KERNEL */

#ifdef TRACE_UST
struct lttng_channel channel_ust;
struct lttng_domain domain_ust;
struct lttng_event event_ust = {0};
struct lttng_handle *handle_ust;

//// Adding ust trace
// Creating the handle
domain_ust.type = LTTNG_DOMAIN_UST;
if ((handle_ust = lttng_create_handle(TRACE_SESSION_NAME, &domain_ust)) == NULL) {
printf("LTTng: error creating handle: %s\n", lttng_strerror(ret));
return 2;
}

// On calcule la taille et le nombre de buffers par rapport au nombre de cycles
#define UST_EVENT_SIZE 100
long nbOfLoops = 100000000;
long totalSize = UST_EVENT_SIZE*nbOfLoops;
long subbufsize = 4096;
long subbufnum = 8;

/*printf("Pouet\n");
int i = 0;
while (subbufnum*subbufsize < totalSize) {
if (i%3 < 2) subbufsize *= 2;
else subbufnum *= 2;
i++;
}*/
printf("Subbuffers : %d * %d\n", subbufnum, subbufsize);

// Enabling the channel
strcpy(channel_ust.name, "xhpchan");
channel_ust.attr.overwrite = 0;
channel_ust.attr.subbuf_size = subbufsize;//4096;
channel_ust.attr.num_subbuf = subbufnum;//8;
channel_ust.attr.switch_timer_interval = 0;
channel_ust.attr.read_timer_interval = 200;
channel_ust.attr.output = LTTNG_EVENT_SPLICE;

if ((ret = lttng_enable_channel(handle_ust, &channel_ust)) < 0) {
printf("LTTng: error enable channel: %s\n", lttng_strerror(ret));
return 3;
}

// Enabling the event
//memset(&event_ust, 0, sizeof(struct lttng_event));
strcpy(event_ust.name, "ust_xhp:xhploop");
event_ust.type = LTTNG_EVENT_TRACEPOINT;
//event_ust.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
//printf("Pouet %d\n", event_ust.loglevel_type);

if ((ret = lttng_enable_event(handle_ust, &event_ust, channel_ust.name)) < 0) {
printf("LTTng: error enabling event: %s\n", lttng_strerror(ret));
return 3;
}
#endif /* TRACE_UST */

//// Start tracing
if ((ret = lttng_start_tracing(TRACE_SESSION_NAME)) < 0) {
printf("LTTng: error starting tracing: %s\n", lttng_strerror(ret));
return 4;
}

tracepoint(ust_xhp, xhploop, 1, 0);

if ((ret = lttng_stop_tracing(TRACE_SESSION_NAME)) < 0) {
printf("LTTng: error stopping tracing (end): %s\n", lttng_strerror(ret));
return 5;
}
if ((ret = lttng_destroy_session(TRACE_SESSION_NAME)) < 0) {
printf("LTTng: error destroying session (end): %s\n", lttng_strerror(ret));
return 6;
}

printf("YABADABADOOOOOOO\n");

return EXIT_SUCCESS;
}

(2-2/4)