Esempio n. 1
0
int
main(int argc, char **argv)
{
	int i;
	const char *colon, *ptag;
	const char *profile = "responsetime";
	pthread_t thr;
	int fnum = -1;
	struct profile cli_p = {0};
	cli_p.name = 0;

	VUT_Init(progname, argc, argv, &vopt_spec);
	AZ(pthread_cond_init(&timebend_cv, NULL));

	while ((i = getopt(argc, argv, vopt_spec.vopt_optstring)) != -1) {
		switch (i) {
		case 'h':
			/* Usage help */
			usage(0);
		case 'p':
			delay = strtod(optarg, NULL);
			if (delay <= 0)
				VUT_Error(1, "-p: invalid '%s'", optarg);
			break;
		case 'P':
			colon = strchr(optarg, ':');
			/* no colon, take the profile as a name */
			if (colon == NULL) {
				profile = optarg;
				break;
			}
			/* else it's a definition, we hope */
			if (colon == optarg + 1 &&
			    (*optarg == 'b' || *optarg == 'c')) {
				cli_p.VSL_arg = *optarg;
				ptag = colon + 1;
				colon = strchr(colon + 1, ':');
				if (colon == NULL)
					profile_error(optarg);
			} else {
				ptag = optarg;
				cli_p.VSL_arg = 'c';
			}

			assert(colon);
			if (sscanf(colon + 1, "%d:%d:%d", &cli_p.field,
			    &cli_p.hist_low, &cli_p.hist_high) != 3)
				profile_error(optarg);

			match_tag = VSL_Name2Tag(ptag, colon - ptag);
			if (match_tag < 0)
				VUT_Error(1,
				    "-P: '%s' is not a valid tag name",
				    optarg);
			cli_p.name = "custom";
			cli_p.tag = match_tag;
			profile = NULL;
			active_profile = &cli_p;

			break;
		case 'B':
			timebend = strtod(optarg, NULL);
			if (timebend == 0)
				VUT_Error(1,
				    "-B: being able to bend time does not"
				    " mean we can stop it"
				    " (invalid factor '%s')", optarg);
			if (timebend < 0)
				VUT_Error(1,
				    "-B: being able to bend time does not"
				    " mean we can make it go backwards"
				    " (invalid factor '%s')", optarg);
			break;
		default:
			if (!VUT_Arg(i, optarg))
				usage(1);
		}
	}

	if (optind != argc)
		usage(1);

	/* Check for valid grouping mode */
	assert(VUT.g_arg < VSL_g__MAX);
	if (VUT.g_arg != VSL_g_vxid && VUT.g_arg != VSL_g_request)
		VUT_Error(1, "Invalid grouping mode: %s"
		    " (only vxid and request are supported)",
		    VSLQ_grouping[VUT.g_arg]);

	if (profile) {
		for (active_profile = profiles; active_profile->name;
		     active_profile++) {
			if (strcmp(active_profile->name, profile) == 0)
				break;
		}
	}
	AN(active_profile);
	if (!active_profile->name)
		VUT_Error(1, "-P: No such profile '%s'", profile);

	assert(VUT_Arg(active_profile->VSL_arg, NULL));
	match_tag = active_profile->tag;
	fnum = active_profile->field;
	hist_low = active_profile->hist_low;
	hist_high = active_profile->hist_high;

	hist_range = hist_high - hist_low;
	hist_buckets = hist_range * HIST_RES;
	bucket_hit = calloc(sizeof *bucket_hit, hist_buckets);
	bucket_miss = calloc(sizeof *bucket_miss, hist_buckets);

	if (timebend > 0)
		t0 = VTIM_mono();

	format = malloc(4L * fnum);
	AN(format);
	for (i = 0; i < fnum - 1; i++)
		strcpy(format + 4 * i, "%*s ");
	strcpy(format + 4 * (fnum - 1), "%lf");

	log_ten = log(10.0);

	VUT_Setup();
	if (pthread_create(&thr, NULL, do_curses, NULL) != 0)
		VUT_Error(1, "pthread_create(): %s", strerror(errno));
	VUT.dispatch_f = accumulate;
	VUT.dispatch_priv = NULL;
	VUT.sighup_f = sighup;
	VUT_Main();
	end_of_file = 1;
	AZ(pthread_join(thr, NULL));
	VUT_Fini();
	exit(0);
}
int
main(int argc, char **argv)
{
    extern int 		optind;
    extern char		*optarg;
#ifdef __OPENSTEP__
    extern int 		getopt(int argc, char **argv, char *optstring);
#endif
    int 		c;
    int 		bflag = 0, hflag = 0, pflag = 0, eflag = 0, dflag = 0;
    int			sflag = 0;
    int 		errflag = 0;
    char 		*ofile = "gmon.out";
    char 		*dylib;
    port_t		server_port;
    port_t		control_port;
    enum profile_state	state;
    enum request_type	request = -1, control_request = -1;
    char		gmon_file[MAXPATHLEN];
    boolean_t		profile_active, control_active;
    enum result_code	result;
    kern_return_t	ret;
    
    progname = argv[0];
    
    if (argc == 1) {
	 usage();
	 exit(2);
    }
    while ((c = getopt(argc, argv, "crbhpo:eds")) != EOF)
        switch (c) {
          case 'c':
            request = NSCreateProfileBufferForLibrary;
            break;
          case 'r':
            request = NSRemoveProfileBufferForLibrary;
            break;
          case 'b':
            bflag = 1;
            request = NSStartProfilingForLibrary;
            break;
          case 'h':
            hflag = 1;
            request = NSStopProfilingForLibrary;
            break;
          case 'p':
	    request = NSBufferFileForLibrary,
            pflag = 1;
            break;
          case 'o':
	    ofile = optarg;
            break;
	  case 'e':
	    eflag = 1;
	    control_request = NSEnableProfiling;
	    break;
	  case 'd':
	    dflag = 1;
	    control_request = NSDisableProfiling;
	    break;
	  case 's':
	    sflag = 1;
	    break;
          default:
            errflag = 1;
            break;
        }
	
    if (dflag && eflag) {
	fprintf(stderr, "You must specify only one of -d and -e.\n");
	usage();
	exit(2);
    }
    if (bflag && hflag) {
	fprintf(stderr, "You must specify only one of -b and -h.\n");
	usage();
	exit(2);
    }
    
    if (errflag || (!(dflag || eflag || sflag) && optind >= argc)) {
	usage();
	exit(2);
    }
    
    dylib = argv[optind];
    
    if (bootstrap_look_up(bootstrap_port, PROFILE_SERVER_NAME, &server_port)
	!= BOOTSTRAP_SUCCESS ||
	bootstrap_status(bootstrap_port, PROFILE_SERVER_NAME, &profile_active)
	!= BOOTSTRAP_SUCCESS) {
	fprintf(stderr, "%s: couldn't locate profile server port\n",progname);
	exit(3);
    }
    
    if (bootstrap_look_up(bootstrap_port, PROFILE_CONTROL_NAME, &control_port)
	!= BOOTSTRAP_SUCCESS ||
	bootstrap_status(bootstrap_port, PROFILE_CONTROL_NAME, &control_active)
	!= BOOTSTRAP_SUCCESS) {
	fprintf(stderr, "%s: couldn't locate profile control port\n",progname);
	exit(3);
    }
    
    if (sflag) {
	if (!control_active) {
	    printf("Profiling service not found\n");
	    exit(0);
	}
	if (!profile_active) {
	    printf("Profiling service not enabled\n");
	    exit(0);
	}
	print_status(server_port);
	exit(0);
    }
    
    *gmon_file = '\0';
    if (dflag || eflag) {
	if (!control_active) {
	    fprintf(stderr, "%s: profile server isn't running\n", progname);
	    exit(3);
	}
	if ((ret = send_request(control_port, control_request, "", &state, &result, gmon_file)) != KERN_SUCCESS) {
	    fprintf(stderr, "%s: msg_send failed: %s", progname, mach_error_string(ret));
	    exit(3);
	}
	if (result != NSSuccess) {
	    profile_error(control_request, dylib, result);
	    exit(3);
	}
	exit(0);
    }

    if (request != -1) {
	if (!profile_active) {
	    fprintf(stderr, "%s: profile server isn't active.\n", progname);
	    fprintf(stderr, "Use '%s -e' to enable it.\n", progname);
	    exit(3);
	}
	if ((ret = send_request(server_port, request, dylib, &state, &result, gmon_file)) != KERN_SUCCESS) {
	    fprintf(stderr, "%s: msg_send failed: %s\n", progname, mach_error_string(ret));
	    exit(3);
	}
	if (result != NSSuccess) {
	    profile_error(request, dylib, result);
	    exit(3);
	}
    }
    
    if (pflag) {	
	if (*gmon_file == '\0' || state == NSBufferNotCreated) {
	    fprintf(stderr, "%s: dylib %s not found\n",progname,dylib);
	    exit(1);
	}
	
	errflag = copy_file(gmon_file, ofile);
	if (errflag) {
	    fprintf(stderr,"%s: couldn't write to %s\n", progname, ofile);
	    exit(errflag);
	}
    }
    
    exit(0);
}