2011-05-10 07:33:19 +08:00
|
|
|
// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
|
2010-04-15 04:32:20 +08:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2010-04-16 07:40:23 +08:00
|
|
|
#include <cstdio>
|
2010-04-15 04:32:20 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include "metrics_library.h"
|
|
|
|
|
2010-10-02 06:38:42 +08:00
|
|
|
void ShowUsage() {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: metrics_client [-ab] [-t] name sample min max nbuckets\n"
|
|
|
|
" metrics_client [-ab] -e name sample max\n"
|
2011-01-07 02:51:47 +08:00
|
|
|
" metrics_client -u action\n"
|
2010-10-02 06:38:42 +08:00
|
|
|
" metrics_client [-cg]\n"
|
|
|
|
"\n"
|
|
|
|
" default: send metric with integer values to Chrome only\n"
|
|
|
|
" |min| > 0, |min| <= sample < |max|\n"
|
|
|
|
" -a: send metric (name/sample) to Autotest only\n"
|
|
|
|
" -b: send metric to both Chrome and Autotest\n"
|
|
|
|
" -c: return exit status 0 if user consents to stats, 1 otherwise\n"
|
|
|
|
" -e: send linear/enumeration histogram data\n"
|
|
|
|
" -g: return exit status 0 if machine in guest mode, 1 otherwise\n"
|
2011-01-07 02:51:47 +08:00
|
|
|
" -t: convert sample from double seconds to int milliseconds\n"
|
|
|
|
" -u: send a user action to Chrome\n");
|
2010-10-02 06:38:42 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2010-04-15 04:32:20 +08:00
|
|
|
|
2010-10-02 06:38:42 +08:00
|
|
|
static int SendStats(char* argv[],
|
|
|
|
int name_index,
|
|
|
|
bool send_enum,
|
|
|
|
bool secs_to_msecs,
|
|
|
|
bool send_to_autotest,
|
|
|
|
bool send_to_chrome) {
|
2010-04-22 05:24:04 +08:00
|
|
|
const char* name = argv[name_index];
|
|
|
|
int sample;
|
2010-04-16 07:40:23 +08:00
|
|
|
if (secs_to_msecs) {
|
2010-04-22 05:24:04 +08:00
|
|
|
sample = static_cast<int>(atof(argv[name_index + 1]) * 1000.0);
|
2010-04-16 07:40:23 +08:00
|
|
|
} else {
|
2010-04-22 05:24:04 +08:00
|
|
|
sample = atoi(argv[name_index + 1]);
|
2010-04-16 07:40:23 +08:00
|
|
|
}
|
|
|
|
|
2010-04-15 04:32:20 +08:00
|
|
|
// Send metrics
|
|
|
|
if (send_to_autotest) {
|
2010-04-22 05:24:04 +08:00
|
|
|
MetricsLibrary::SendToAutotest(name, sample);
|
2010-04-15 04:32:20 +08:00
|
|
|
}
|
2010-04-22 06:45:10 +08:00
|
|
|
|
2010-04-15 04:32:20 +08:00
|
|
|
if (send_to_chrome) {
|
2010-05-13 06:26:16 +08:00
|
|
|
MetricsLibrary metrics_lib;
|
|
|
|
metrics_lib.Init();
|
2010-04-22 06:45:10 +08:00
|
|
|
if (send_enum) {
|
|
|
|
int max = atoi(argv[name_index + 2]);
|
2010-05-13 06:26:16 +08:00
|
|
|
metrics_lib.SendEnumToUMA(name, sample, max);
|
2010-04-22 06:45:10 +08:00
|
|
|
} else {
|
|
|
|
int min = atoi(argv[name_index + 2]);
|
|
|
|
int max = atoi(argv[name_index + 3]);
|
|
|
|
int nbuckets = atoi(argv[name_index + 4]);
|
2010-05-13 06:26:16 +08:00
|
|
|
metrics_lib.SendToUMA(name, sample, min, max, nbuckets);
|
2010-04-22 06:45:10 +08:00
|
|
|
}
|
2010-04-15 04:32:20 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2010-10-02 06:38:42 +08:00
|
|
|
|
2011-01-07 02:51:47 +08: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;
|
|
|
|
}
|
|
|
|
|
2010-10-02 06:38:42 +08: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) {
|
|
|
|
enum Mode {
|
|
|
|
kModeSendStats,
|
2011-01-07 02:51:47 +08:00
|
|
|
kModeSendUserAction,
|
2010-10-02 06:38:42 +08:00
|
|
|
kModeHasConsent,
|
|
|
|
kModeIsGuestMode
|
|
|
|
} mode = kModeSendStats;
|
|
|
|
bool send_to_autotest = false;
|
|
|
|
bool send_to_chrome = true;
|
|
|
|
bool send_enum = false;
|
|
|
|
bool secs_to_msecs = false;
|
|
|
|
|
|
|
|
// Parse arguments
|
|
|
|
int flag;
|
2011-01-07 02:51:47 +08:00
|
|
|
while ((flag = getopt(argc, argv, "abcegtu")) != -1) {
|
2010-10-02 06:38:42 +08:00
|
|
|
switch (flag) {
|
|
|
|
case 'a':
|
|
|
|
mode = kModeSendStats;
|
|
|
|
send_to_autotest = true;
|
|
|
|
send_to_chrome = false;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
mode = kModeSendStats;
|
|
|
|
send_to_chrome = true;
|
|
|
|
send_to_autotest = true;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
mode = kModeHasConsent;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
send_enum = true;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
mode = kModeIsGuestMode;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
secs_to_msecs = true;
|
|
|
|
break;
|
2011-01-07 02:51:47 +08:00
|
|
|
case 'u':
|
|
|
|
mode = kModeSendUserAction;
|
|
|
|
break;
|
2010-10-02 06:38:42 +08:00
|
|
|
default:
|
2011-05-10 07:33:19 +08:00
|
|
|
ShowUsage();
|
2010-10-02 06:38:42 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-01-07 02:51:47 +08:00
|
|
|
int arg_index = optind;
|
2010-10-02 06:38:42 +08:00
|
|
|
|
|
|
|
int expected_args = 0;
|
|
|
|
if (mode == kModeSendStats)
|
|
|
|
expected_args = send_enum ? 3 : 5;
|
2011-01-07 02:51:47 +08:00
|
|
|
else if (mode == kModeSendUserAction)
|
|
|
|
expected_args = 1;
|
2010-10-02 06:38:42 +08:00
|
|
|
|
2011-01-07 02:51:47 +08:00
|
|
|
if ((arg_index + expected_args) != argc) {
|
2010-10-02 06:38:42 +08:00
|
|
|
ShowUsage();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(mode) {
|
|
|
|
case kModeSendStats:
|
|
|
|
if (send_enum && secs_to_msecs) {
|
|
|
|
ShowUsage();
|
|
|
|
}
|
|
|
|
return SendStats(argv,
|
2011-01-07 02:51:47 +08:00
|
|
|
arg_index,
|
2010-10-02 06:38:42 +08:00
|
|
|
send_enum,
|
|
|
|
secs_to_msecs,
|
|
|
|
send_to_autotest,
|
|
|
|
send_to_chrome);
|
2011-01-07 02:51:47 +08:00
|
|
|
case kModeSendUserAction:
|
|
|
|
return SendUserAction(argv, arg_index);
|
2010-10-02 06:38:42 +08:00
|
|
|
case kModeHasConsent:
|
|
|
|
return HasConsent();
|
|
|
|
case kModeIsGuestMode:
|
|
|
|
return IsGuestMode();
|
|
|
|
default:
|
|
|
|
ShowUsage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|