blob: 655c174b2f5262008c9c3b0d374e849378a32aeb [file] [log] [blame]
Lakshmi Narayana Kalavala25fe12c2019-12-11 20:44:55 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <signal.h>
18#include <unistd.h>
19#include <chrono>
20#include <fstream>
21#include <iostream>
22#include <thread>
23
24#include "histogram_collector.h"
25
26void sigint_handler(int) {}
27
28void show_usage(char *progname) {
29 std::cout << "Usage: ./" + std::string(progname) + " {options} \n"
30 << "Sample the V (as in HSV) channel of the pixels that were displayed onscreen.\n\n"
31 << "\tOptions:\n"
32 << "\t-h display this help message\n"
33 << "\t-o write output to specified filename\n"
34 << "\t-t NUM Collect results over NUM seconds, and then exit\n"
35 << "\t-m NUM Only store the last NUM frames of statistics\n";
36}
37
38int main(int argc, char **argv) {
39 struct sigaction sigHandler;
40 sigHandler.sa_handler = sigint_handler;
41 sigemptyset(&sigHandler.sa_mask);
42 sigHandler.sa_flags = 0;
43 sigaction(SIGINT, &sigHandler, NULL);
44
45 int c;
46 char *output_filename = NULL;
47 int timeout = -1;
48 while ((c = getopt(argc, argv, "o:t:h")) != -1) {
49 switch (c) {
50 case 'o':
51 output_filename = optarg;
52 break;
53 case 't':
54 timeout = strtol(optarg, NULL, 10);
55 break;
56 default:
57 case 'h':
58 show_usage(argv[0]);
59 return EXIT_SUCCESS;
60 }
61 }
62
63 histogram::HistogramCollector histogram;
64 histogram.start();
65
66 bool cancelled_during_wait = false;
67 if (timeout > 0) {
68 std::cout << "Sampling for " << timeout << " seconds.\n";
69 struct timespec request, remaining;
70 request.tv_sec = timeout;
71 request.tv_nsec = 0;
72 cancelled_during_wait = (nanosleep(&request, &remaining) != 0);
73 } else {
74 std::cout << "Sampling until Ctrl-C is pressed\n";
75 sigsuspend(&sigHandler.sa_mask);
76 }
77
78 std::cout << "Sampling results:\n";
79
80 histogram.stop();
81
82 if (cancelled_during_wait) {
83 std::cout << "Timed histogram collection cancelled via signal\n";
84 return EXIT_SUCCESS;
85 }
86
87 if (output_filename) {
88 std::cout << "\nWriting statistics to: " << output_filename << '\n';
89 std::ofstream output_file;
90 output_file.open(output_filename);
91 if (!output_file.is_open()) {
92 std::cerr << "Error, could not open given file: " << output_filename << "\n";
93 return EXIT_FAILURE;
94 }
95 output_file << histogram.Dump();
96 output_file.close();
97 } else {
98 std::cout << histogram.Dump() << '\n';
99 }
100
101 return EXIT_SUCCESS;
102}