2011-05-10 01:33:19 +02:00
|
|
|
// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
|
2010-04-14 22:32:20 +02:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2010-04-16 01:40:23 +02:00
|
|
|
#include <cstdio>
|
2010-04-14 22:32:20 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2014-07-10 01:35:23 +02:00
|
|
|
#include "metrics/metrics_library.h"
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2013-03-19 23:02:42 +01:00
|
|
|
enum Mode {
|
|
|
|
kModeSendSample,
|
|
|
|
kModeSendEnumSample,
|
|
|
|
kModeSendSparseSample,
|
|
|
|
kModeSendUserAction,
|
|
|
|
kModeSendCrosEvent,
|
|
|
|
kModeHasConsent,
|
|
|
|
kModeIsGuestMode,
|
|
|
|
};
|
|
|
|
|
2010-10-02 00:38:42 +02:00
|
|
|
void ShowUsage() {
|
|
|
|
fprintf(stderr,
|
2015-08-04 23:10:10 +02:00
|
|
|
"Usage: metrics_client [-t] name sample min max nbuckets\n"
|
|
|
|
" metrics_client -e name sample max\n"
|
|
|
|
" metrics_client -s name sample\n"
|
|
|
|
" metrics_client -v event\n"
|
2011-01-06 19:51:47 +01:00
|
|
|
" metrics_client -u action\n"
|
2010-10-02 00:38:42 +02:00
|
|
|
" metrics_client [-cg]\n"
|
|
|
|
"\n"
|
2015-08-04 23:10:10 +02:00
|
|
|
" default: send metric with integer values \n"
|
2010-10-02 00:38:42 +02:00
|
|
|
" |min| > 0, |min| <= sample < |max|\n"
|
2014-08-21 15:00:50 +02:00
|
|
|
" -c: return exit status 0 if user consents to stats, 1 otherwise,\n"
|
|
|
|
" in guest mode always return 1\n"
|
2010-10-02 00:38:42 +02:00
|
|
|
" -e: send linear/enumeration histogram data\n"
|
|
|
|
" -g: return exit status 0 if machine in guest mode, 1 otherwise\n"
|
2013-03-19 23:02:42 +01:00
|
|
|
" -s: send a sparse histogram sample\n"
|
2011-01-06 19:51:47 +01:00
|
|
|
" -t: convert sample from double seconds to int milliseconds\n"
|
2013-03-13 18:53:55 +01:00
|
|
|
" -u: send a user action to Chrome\n"
|
2014-07-10 01:35:23 +02:00
|
|
|
" -v: send a Platform.CrOSEvent enum histogram sample\n");
|
2010-10-02 00:38:42 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2014-03-27 22:19:06 +01:00
|
|
|
static int ParseInt(const char *arg) {
|
|
|
|
char *endptr;
|
|
|
|
int value = strtol(arg, &endptr, 0);
|
|
|
|
if (*endptr != '\0') {
|
|
|
|
fprintf(stderr, "metrics client: bad integer \"%s\"\n", arg);
|
|
|
|
ShowUsage();
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double ParseDouble(const char *arg) {
|
|
|
|
char *endptr;
|
|
|
|
double value = strtod(arg, &endptr);
|
|
|
|
if (*endptr != '\0') {
|
|
|
|
fprintf(stderr, "metrics client: bad double \"%s\"\n", arg);
|
|
|
|
ShowUsage();
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2010-10-02 00:38:42 +02:00
|
|
|
static int SendStats(char* argv[],
|
|
|
|
int name_index,
|
2013-03-19 23:02:42 +01:00
|
|
|
enum Mode mode,
|
2015-08-04 23:10:10 +02:00
|
|
|
bool secs_to_msecs) {
|
2010-04-21 23:24:04 +02:00
|
|
|
const char* name = argv[name_index];
|
|
|
|
int sample;
|
2010-04-16 01:40:23 +02:00
|
|
|
if (secs_to_msecs) {
|
2014-03-27 22:19:06 +01:00
|
|
|
sample = static_cast<int>(ParseDouble(argv[name_index + 1]) * 1000.0);
|
2010-04-16 01:40:23 +02:00
|
|
|
} else {
|
2014-03-27 22:19:06 +01:00
|
|
|
sample = ParseInt(argv[name_index + 1]);
|
2010-04-16 01:40:23 +02:00
|
|
|
}
|
|
|
|
|
2015-08-04 23:10:10 +02:00
|
|
|
MetricsLibrary metrics_lib;
|
|
|
|
metrics_lib.Init();
|
|
|
|
if (mode == kModeSendSparseSample) {
|
|
|
|
metrics_lib.SendSparseToUMA(name, sample);
|
|
|
|
} else if (mode == kModeSendEnumSample) {
|
|
|
|
int max = ParseInt(argv[name_index + 2]);
|
|
|
|
metrics_lib.SendEnumToUMA(name, sample, max);
|
|
|
|
} else {
|
|
|
|
int min = ParseInt(argv[name_index + 2]);
|
|
|
|
int max = ParseInt(argv[name_index + 3]);
|
|
|
|
int nbuckets = ParseInt(argv[name_index + 4]);
|
|
|
|
metrics_lib.SendToUMA(name, sample, min, max, nbuckets);
|
2010-04-14 22:32:20 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2010-10-02 00:38:42 +02:00
|
|
|
|
2011-01-06 19:51:47 +01:00
|
|
|
static int SendUserAction(char* argv[], int action_index) {
|
|
|
|
const char* action = argv[action_index];
|
|
|
|
MetricsLibrary metrics_lib;
|
|
|
|
metrics_lib.Init();
|
|
|
|
metrics_lib.SendUserActionToUMA(action);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-13 18:53:55 +01:00
|
|
|
static int SendCrosEvent(char* argv[], int action_index) {
|
|
|
|
const char* event = argv[action_index];
|
|
|
|
bool result;
|
|
|
|
MetricsLibrary metrics_lib;
|
|
|
|
metrics_lib.Init();
|
|
|
|
result = metrics_lib.SendCrosEventToUMA(event);
|
|
|
|
if (!result) {
|
|
|
|
fprintf(stderr, "metrics_client: could not send event %s\n", event);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-02 00:38:42 +02:00
|
|
|
static int HasConsent() {
|
|
|
|
MetricsLibrary metrics_lib;
|
|
|
|
metrics_lib.Init();
|
|
|
|
return metrics_lib.AreMetricsEnabled() ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int IsGuestMode() {
|
|
|
|
MetricsLibrary metrics_lib;
|
|
|
|
metrics_lib.Init();
|
|
|
|
return metrics_lib.IsGuestMode() ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2013-03-19 23:02:42 +01:00
|
|
|
enum Mode mode = kModeSendSample;
|
2010-10-02 00:38:42 +02:00
|
|
|
bool secs_to_msecs = false;
|
|
|
|
|
|
|
|
// Parse arguments
|
|
|
|
int flag;
|
2013-03-19 23:02:42 +01:00
|
|
|
while ((flag = getopt(argc, argv, "abcegstuv")) != -1) {
|
2010-10-02 00:38:42 +02:00
|
|
|
switch (flag) {
|
|
|
|
case 'c':
|
|
|
|
mode = kModeHasConsent;
|
|
|
|
break;
|
|
|
|
case 'e':
|
2013-03-19 23:02:42 +01:00
|
|
|
mode = kModeSendEnumSample;
|
2010-10-02 00:38:42 +02:00
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
mode = kModeIsGuestMode;
|
|
|
|
break;
|
2013-03-19 23:02:42 +01:00
|
|
|
case 's':
|
|
|
|
mode = kModeSendSparseSample;
|
|
|
|
break;
|
2010-10-02 00:38:42 +02:00
|
|
|
case 't':
|
|
|
|
secs_to_msecs = true;
|
|
|
|
break;
|
2011-01-06 19:51:47 +01:00
|
|
|
case 'u':
|
|
|
|
mode = kModeSendUserAction;
|
|
|
|
break;
|
2013-03-13 18:53:55 +01:00
|
|
|
case 'v':
|
|
|
|
mode = kModeSendCrosEvent;
|
|
|
|
break;
|
2010-10-02 00:38:42 +02:00
|
|
|
default:
|
2011-05-10 01:33:19 +02:00
|
|
|
ShowUsage();
|
2010-10-02 00:38:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-01-06 19:51:47 +01:00
|
|
|
int arg_index = optind;
|
2010-10-02 00:38:42 +02:00
|
|
|
|
|
|
|
int expected_args = 0;
|
2013-03-19 23:02:42 +01:00
|
|
|
if (mode == kModeSendSample)
|
|
|
|
expected_args = 5;
|
|
|
|
else if (mode == kModeSendEnumSample)
|
|
|
|
expected_args = 3;
|
|
|
|
else if (mode == kModeSendSparseSample)
|
|
|
|
expected_args = 2;
|
2011-01-06 19:51:47 +01:00
|
|
|
else if (mode == kModeSendUserAction)
|
|
|
|
expected_args = 1;
|
2013-03-13 18:53:55 +01:00
|
|
|
else if (mode == kModeSendCrosEvent)
|
|
|
|
expected_args = 1;
|
2010-10-02 00:38:42 +02:00
|
|
|
|
2011-01-06 19:51:47 +01:00
|
|
|
if ((arg_index + expected_args) != argc) {
|
2010-10-02 00:38:42 +02:00
|
|
|
ShowUsage();
|
|
|
|
}
|
|
|
|
|
2014-07-10 01:35:23 +02:00
|
|
|
switch (mode) {
|
2013-03-19 23:02:42 +01:00
|
|
|
case kModeSendSample:
|
|
|
|
case kModeSendEnumSample:
|
|
|
|
case kModeSendSparseSample:
|
|
|
|
if ((mode != kModeSendSample) && secs_to_msecs) {
|
2010-10-02 00:38:42 +02:00
|
|
|
ShowUsage();
|
|
|
|
}
|
|
|
|
return SendStats(argv,
|
2011-01-06 19:51:47 +01:00
|
|
|
arg_index,
|
2013-03-19 23:02:42 +01:00
|
|
|
mode,
|
2015-08-04 23:10:10 +02:00
|
|
|
secs_to_msecs);
|
2011-01-06 19:51:47 +01:00
|
|
|
case kModeSendUserAction:
|
|
|
|
return SendUserAction(argv, arg_index);
|
2013-03-13 18:53:55 +01:00
|
|
|
case kModeSendCrosEvent:
|
|
|
|
return SendCrosEvent(argv, arg_index);
|
2010-10-02 00:38:42 +02:00
|
|
|
case kModeHasConsent:
|
|
|
|
return HasConsent();
|
|
|
|
case kModeIsGuestMode:
|
|
|
|
return IsGuestMode();
|
|
|
|
default:
|
|
|
|
ShowUsage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|