Home | History | Annotate | Download | only in metric
      1 /*
      2  * Copyright (C) 2019 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 package android.cts.statsd.metric;
     17 
     18 import android.cts.statsd.atom.DeviceAtomTestCase;
     19 
     20 import com.android.internal.os.StatsdConfigProto;
     21 import com.android.internal.os.StatsdConfigProto.ActivationType;
     22 import com.android.internal.os.StatsdConfigProto.AtomMatcher;
     23 import com.android.internal.os.StatsdConfigProto.EventActivation;
     24 import com.android.internal.os.StatsdConfigProto.EventMetric;
     25 import com.android.internal.os.StatsdConfigProto.GaugeMetric;
     26 import com.android.internal.os.StatsdConfigProto.MetricActivation;
     27 import com.android.internal.os.StatsdConfigProto.StatsdConfig;
     28 import com.android.os.AtomsProto.AppBreadcrumbReported;
     29 import com.android.os.AtomsProto.Atom;
     30 import com.android.os.StatsLog.ConfigMetricsReport;
     31 import com.android.os.StatsLog.ConfigMetricsReportList;
     32 import com.android.os.StatsLog.StatsLogReport;
     33 import com.android.tradefed.log.LogUtil;
     34 
     35 /**
     36  * Test Statsd Metric activations and deactivations
     37  */
     38 public class MetricActivationTests extends DeviceAtomTestCase {
     39     private final long metric1Id = 1L;
     40     private final int metric1MatcherId = 1;
     41 
     42     private final long metric2Id = 2L;
     43     private final int metric2MatcherId = 2;
     44 
     45     private final long metric3Id = 3L;
     46     private final int metric3MatcherId = 3;
     47 
     48     private final int act1MatcherId = 10;
     49     private final int act1CancelMatcherId = -10;
     50 
     51     private final int act2MatcherId = 20;
     52     private final int act2CancelMatcherId = -20;
     53 
     54 
     55     private StatsdConfig.Builder createConfig(final int act1TtlSecs, final int act2TtlSecs) {
     56         AtomMatcher metric1Matcher =
     57                 MetricsUtils.simpleAtomMatcher(metric1MatcherId, metric1MatcherId);
     58         AtomMatcher metric2Matcher =
     59                 MetricsUtils.simpleAtomMatcher(metric2MatcherId, metric2MatcherId);
     60         AtomMatcher metric3Matcher =
     61                 MetricsUtils.simpleAtomMatcher(metric3MatcherId, metric3MatcherId);
     62         AtomMatcher act1Matcher =
     63                 MetricsUtils.simpleAtomMatcher(act1MatcherId, act1MatcherId);
     64         AtomMatcher act1CancelMatcher =
     65                 MetricsUtils.simpleAtomMatcher(act1CancelMatcherId, act1CancelMatcherId);
     66         AtomMatcher act2Matcher =
     67                 MetricsUtils.simpleAtomMatcher(act2MatcherId, act2MatcherId);
     68         AtomMatcher act2CancelMatcher =
     69                 MetricsUtils.simpleAtomMatcher(act2CancelMatcherId, act2CancelMatcherId);
     70 
     71         EventMetric metric1 = EventMetric.newBuilder()
     72                 .setId(metric1Id)
     73                 .setWhat(metric1MatcherId)
     74                 .build();
     75 
     76         EventMetric metric2 = EventMetric.newBuilder()
     77                 .setId(metric2Id)
     78                 .setWhat(metric2MatcherId)
     79                 .build();
     80 
     81         EventMetric metric3 = EventMetric.newBuilder()
     82                 .setId(metric3Id)
     83                 .setWhat(metric3MatcherId)
     84                 .build();
     85 
     86         EventActivation metric1Act1 =
     87                 MetricsUtils.createEventActivation(act1TtlSecs, act1MatcherId, act1CancelMatcherId)
     88                     .build();
     89 
     90         EventActivation metric1Act2 =
     91                 MetricsUtils.createEventActivation(act2TtlSecs, act2MatcherId, act2CancelMatcherId)
     92                     .setActivationType(ActivationType.ACTIVATE_ON_BOOT)
     93                     .build();
     94 
     95         EventActivation metric2Act1 =
     96                 MetricsUtils.createEventActivation(act1TtlSecs, act1MatcherId, act1CancelMatcherId)
     97                     .setActivationType(ActivationType.ACTIVATE_ON_BOOT)
     98                     .build();
     99 
    100         EventActivation metric2Act2 =
    101                 MetricsUtils.createEventActivation(act2TtlSecs, act2MatcherId, act2CancelMatcherId)
    102                     .setActivationType(ActivationType.ACTIVATE_IMMEDIATELY)
    103                     .build();
    104 
    105         MetricActivation metric1Activation = MetricActivation.newBuilder()
    106                 .setMetricId(metric1Id)
    107                 .setActivationType(ActivationType.ACTIVATE_IMMEDIATELY)
    108                 .addEventActivation(metric1Act1)
    109                 .addEventActivation(metric1Act2)
    110                 .build();
    111 
    112         MetricActivation metric2Activation = MetricActivation.newBuilder()
    113                 .setMetricId(metric2Id)
    114                 .addEventActivation(metric2Act1)
    115                 .addEventActivation(metric2Act2)
    116                 .build();
    117 
    118         return createConfigBuilder()
    119                 .addAtomMatcher(metric1Matcher)
    120                 .addAtomMatcher(metric2Matcher)
    121                 .addAtomMatcher(metric3Matcher)
    122                 .addAtomMatcher(act1Matcher)
    123                 .addAtomMatcher(act1CancelMatcher)
    124                 .addAtomMatcher(act2Matcher)
    125                 .addAtomMatcher(act2CancelMatcher)
    126                 .addEventMetric(metric1)
    127                 .addEventMetric(metric2)
    128                 .addEventMetric(metric3)
    129                 .addMetricActivation(metric1Activation)
    130                 .addMetricActivation(metric2Activation);
    131     }
    132 
    133     /**
    134      * Metric 1:
    135      *     Activation 1:
    136      *         - Ttl: 5 seconds
    137      *         - Type: IMMEDIATE
    138      *     Activation 2:
    139      *         - Ttl: 8 seconds
    140      *         - Type: ON_BOOT
    141      *
    142      * Metric 2:
    143      *     Activation 1:
    144      *         - Ttl: 5 seconds
    145      *         - Type: ON_BOOT
    146      *     Activation 2:
    147      *         - Ttl: 8 seconds
    148      *         - Type: IMMEDIATE
    149      *
    150      * Metric 3: No activations; always active
    151      **/
    152     public void testCancellation() throws Exception {
    153         if (statsdDisabled()) {
    154             return;
    155         }
    156 
    157         final int act1TtlSecs = 5;
    158         final int act2TtlSecs = 8;
    159         uploadConfig(createConfig(act1TtlSecs, act2TtlSecs));
    160 
    161         // Ignored, metric not active.
    162         doAppBreadcrumbReported(metric1MatcherId);
    163         Thread.sleep(10L);
    164 
    165         // Trigger cancel for already inactive event activation 1.
    166         doAppBreadcrumbReported(act1CancelMatcherId);
    167         Thread.sleep(10L);
    168 
    169         // Trigger event activation 1.
    170         doAppBreadcrumbReported(act1MatcherId);
    171         Thread.sleep(10L);
    172 
    173         // First logged event.
    174         doAppBreadcrumbReported(metric1MatcherId);
    175         Thread.sleep(10L);
    176 
    177         // Second logged event.
    178         doAppBreadcrumbReported(metric1MatcherId);
    179         Thread.sleep(10L);
    180 
    181         // Cancel event activation 1.
    182         doAppBreadcrumbReported(act1CancelMatcherId);
    183         Thread.sleep(10L);
    184 
    185         // Ignored, metric not active.
    186         doAppBreadcrumbReported(metric1MatcherId);
    187         Thread.sleep(10L);
    188 
    189         // Trigger event activation 1.
    190         doAppBreadcrumbReported(act1MatcherId);
    191         Thread.sleep(10L);
    192 
    193         // Trigger event activation 2.
    194         doAppBreadcrumbReported(act2MatcherId);
    195         Thread.sleep(10L);
    196 
    197         // Third logged event.
    198         doAppBreadcrumbReported(metric1MatcherId);
    199         Thread.sleep(10L);
    200 
    201         // Cancel event activation 2.
    202         doAppBreadcrumbReported(act2CancelMatcherId);
    203         Thread.sleep(10L);
    204 
    205         // Fourth logged event.
    206         doAppBreadcrumbReported(metric1MatcherId);
    207         Thread.sleep(10L);
    208 
    209         // Expire event activation 1
    210         Thread.sleep(act1TtlSecs * 1000);
    211 
    212         // Ignored, metric 1 not active. Activation 1 expired and Activation 2 was cancelled.
    213         doAppBreadcrumbReported(metric1MatcherId);
    214         Thread.sleep(10L);
    215 
    216         // Trigger event activation 2.
    217         doAppBreadcrumbReported(act2MatcherId);
    218         Thread.sleep(10L);
    219 
    220         // Metric 1 log ignored, Activation 1 expired and Activation 2 needs reboot to activate.
    221         doAppBreadcrumbReported(metric1MatcherId);
    222         Thread.sleep(10L);
    223 
    224         // First logged event for Metric 3.
    225         doAppBreadcrumbReported(metric3MatcherId);
    226         Thread.sleep(10L);
    227 
    228         ConfigMetricsReportList reportList = getReportList();
    229         assertEquals(1, reportList.getReportsCount());
    230         ConfigMetricsReport report = reportList.getReports(0);
    231         verifyMetrics(report, 4, 0, 1);
    232     }
    233 
    234     /**
    235      * Metric 1:
    236      *     Activation 1:
    237      *         - Ttl: 100 seconds
    238      *         - Type: IMMEDIATE
    239      *     Activation 2:
    240      *         - Ttl: 200 seconds
    241      *         - Type: ON_BOOT
    242      *
    243      * Metric 2:
    244      *     Activation 1:
    245      *         - Ttl: 100 seconds
    246      *         - Type: ON_BOOT
    247      *     Activation 2:
    248      *         - Ttl: 200 seconds
    249      *         - Type: IMMEDIATE
    250      *
    251      * Metric 3: No activations; always active
    252      **/
    253     public void testRestart() throws Exception {
    254         if (statsdDisabled()) {
    255             return;
    256         }
    257 
    258         final int act1TtlSecs = 100;
    259         final int act2TtlSecs = 200;
    260         uploadConfig(createConfig(act1TtlSecs, act2TtlSecs));
    261 
    262         // Trigger Metric 1 Activation 1 and Metric 2 Activation 1.
    263         // Time remaining:
    264         // Metric 1 Activation 1: 100 seconds
    265         // Metric 1 Activation 2: 0 seconds
    266         // Metric 2 Activation 1: 0 seconds (will activate after boot)
    267         // Metric 2 Activation 2: 0 seconds
    268         doAppBreadcrumbReported(act1MatcherId);
    269         Thread.sleep(10L);
    270 
    271         // First logged event for Metric 1.
    272         // Metric 2 event ignored, will activate after boot.
    273         // First logged event for Metric 3.
    274         logAllMetrics();
    275 
    276         // Time remaining:
    277         // Metric 1 Activation 1: 100 seconds
    278         // Metric 1 Activation 2: 0 seconds
    279         // Metric 2 Activation 1: 100 seconds
    280         // Metric 2 Activation 2: 0 seconds
    281         rebootDeviceAndWaitUntilReady();
    282 
    283         // Second logged event for Metric 1.
    284         // First logged event for Metric 2.
    285         // Second logged event for Metric 3.
    286         logAllMetrics();
    287 
    288         // Time remaining:
    289         // Metric 1 Activation 1: 0 seconds
    290         // Metric 1 Activation 2: 0 seconds
    291         // Metric 2 Activation 1: 0 seconds
    292         // Metric 2 Activation 2: 0 seconds
    293         Thread.sleep(act1TtlSecs * 1000L);
    294 
    295         // Metric 1 event ignored, Activation 1 expired.
    296         // Metric 2 event ignored, Activation 1 expired.
    297         // Third logged event for Metric 3.
    298         logAllMetrics();
    299 
    300         // Trigger Metric 1 Activation 2 and Metric 2 Activation 2.
    301         // Time remaining:
    302         // Metric 1 Activation 1: 0 seconds
    303         // Metric 1 Activation 2: 0 seconds (will activate after boot)
    304         // Metric 2 Activation 1: 0 seconds
    305         // Metric 2 Activation 2: 200 seconds
    306         doAppBreadcrumbReported(act2MatcherId);
    307         Thread.sleep(10L);
    308 
    309         // Metric 1 event ignored, will activate after boot.
    310         // Second logged event for Metric 2.
    311         // Fourth logged event for Metric 3.
    312         logAllMetrics();
    313 
    314         // Trigger Metric 1 Activation 1 and Metric 2 Activation 1.
    315         // Time remaining:
    316         // Metric 1 Activation 1: 100 seconds
    317         // Metric 1 Activation 2: 0 seconds (will activate after boot)
    318         // Metric 2 Activation 1: 0 seconds (will activate after boot)
    319         // Metric 2 Activation 2: 200 seconds
    320         doAppBreadcrumbReported(act1MatcherId);
    321         Thread.sleep(10L);
    322 
    323         // Third logged event for Metric 1.
    324         // Third logged event for Metric 2.
    325         // Fifth logged event for Metric 3.
    326         logAllMetrics();
    327 
    328         // Time remaining:
    329         // Metric 1 Activation 1: 50 seconds
    330         // Metric 1 Activation 2: 0 seconds (will activate after boot)
    331         // Metric 2 Activation 1: 0 seconds (will activate after boot)
    332         // Metric 2 Activation 2: 150 seconds
    333         Thread.sleep(act1TtlSecs * 1000L / 2);
    334 
    335         // Time remaining:
    336         // Metric 1 Activation 1: 50 seconds
    337         // Metric 1 Activation 2: 200 seconds
    338         // Metric 2 Activation 1: 100 seconds
    339         // Metric 2 Activation 2: 150 seconds
    340         rebootDeviceAndWaitUntilReady();
    341 
    342         // Fourth logged event for Metric 1.
    343         // Fourth logged event for Metric 2.
    344         // Sixth logged event for Metric 3.
    345         logAllMetrics();
    346 
    347         // Expire Metric 1 Activation 1.
    348         // Time remaining:
    349         // Metric 1 Activation 1: 0 seconds
    350         // Metric 1 Activation 2: 150 seconds
    351         // Metric 2 Activation 1: 50 seconds
    352         // Metric 2 Activation 2: 100 seconds
    353         Thread.sleep(act1TtlSecs * 1000L / 2);
    354 
    355         // Fifth logged event for Metric 1.
    356         // Fifth logged event for Metric 2.
    357         // Seventh logged event for Metric 3.
    358         logAllMetrics();
    359 
    360         // Expire all activations.
    361         // Time remaining:
    362         // Metric 1 Activation 1: 0 seconds
    363         // Metric 1 Activation 2: 0 seconds
    364         // Metric 2 Activation 1: 0 seconds
    365         // Metric 2 Activation 2: 0 seconds
    366         Thread.sleep(act2TtlSecs * 1000L);
    367 
    368         // Metric 1 event ignored.
    369         // Metric 2 event ignored.
    370         // Eighth logged event for Metric 3.
    371         logAllMetrics();
    372 
    373         ConfigMetricsReportList reportList = getReportList();
    374         assertEquals(3, reportList.getReportsCount());
    375 
    376         // Report before restart.
    377         ConfigMetricsReport report = reportList.getReports(0);
    378         verifyMetrics(report, 1, 0, 1);
    379 
    380         // Report after first restart.
    381         report = reportList.getReports(1);
    382         verifyMetrics(report, 2, 3, 4);
    383 
    384         // Report after second restart.
    385         report = reportList.getReports(2);
    386         verifyMetrics(report, 2, 2, 3);
    387     }
    388 
    389     /**
    390      * Metric 1:
    391      *     Activation 1:
    392      *         - Ttl: 100 seconds
    393      *         - Type: IMMEDIATE
    394      *     Activation 2:
    395      *         - Ttl: 200 seconds
    396      *         - Type: ON_BOOT
    397      *
    398      * Metric 2:
    399      *     Activation 1:
    400      *         - Ttl: 100 seconds
    401      *         - Type: ON_BOOT
    402      *     Activation 2:
    403      *         - Ttl: 200 seconds
    404      *         - Type: IMMEDIATE
    405      *
    406      * Metric 3: No activations; always active
    407      **/
    408     public void testMultipleActivations() throws Exception {
    409         if (statsdDisabled()) {
    410             return;
    411         }
    412 
    413         final int act1TtlSecs = 100;
    414         final int act2TtlSecs = 200;
    415         uploadConfig(createConfig(act1TtlSecs, act2TtlSecs));
    416 
    417         // Trigger Metric 1 Activation 1 and Metric 2 Activation 1.
    418         // Time remaining:
    419         // Metric 1 Activation 1: 100 seconds
    420         // Metric 1 Activation 2: 0 seconds
    421         // Metric 2 Activation 1: 0 seconds (will activate after boot)
    422         // Metric 2 Activation 2: 0 seconds
    423         doAppBreadcrumbReported(act1MatcherId);
    424         Thread.sleep(10L);
    425 
    426         // First logged event for Metric 1.
    427         // Metric 2 event ignored, will activate after boot.
    428         // First logged event for Metric 3.
    429         logAllMetrics();
    430 
    431         // Time remaining:
    432         // Metric 1 Activation 1: 50 seconds
    433         // Metric 1 Activation 2: 0 seconds
    434         // Metric 2 Activation 1: 0 seconds (will activate after boot)
    435         // Metric 2 Activation 2: 0 seconds
    436         Thread.sleep(act1TtlSecs * 1000L / 2);
    437 
    438         // Second logged event for Metric 1.
    439         // Metric 2 event ignored, will activate after boot.
    440         // Second logged event for Metric 3.
    441         logAllMetrics();
    442 
    443         // Trigger Metric 1 Activation 1 and Metric 2 Activation 1.
    444         // Time remaining:
    445         // Metric 1 Activation 1: 100 seconds
    446         // Metric 1 Activation 2: 0 seconds
    447         // Metric 2 Activation 1: 0 seconds (will activate after boot)
    448         // Metric 2 Activation 2: 0 seconds
    449         doAppBreadcrumbReported(act1MatcherId);
    450         Thread.sleep(10L);
    451 
    452         // Third logged event for Metric 1.
    453         // Metric 2 event ignored, will activate after boot.
    454         // Third logged event for Metric 3.
    455         logAllMetrics();
    456 
    457         // Time remaining:
    458         // Metric 1 Activation 1: 100 seconds
    459         // Metric 1 Activation 2: 0 seconds
    460         // Metric 2 Activation 1: 100 seconds
    461         // Metric 2 Activation 2: 0 seconds
    462         rebootDeviceAndWaitUntilReady();
    463 
    464         // Fourth logged event for Metric 1.
    465         // First logged event for Metric 2.
    466         // Fourth logged event for Metric 3.
    467         logAllMetrics();
    468 
    469         // Trigger Metric 1 Activation 1 and Metric 2 Activation 1.
    470         // Time remaining:
    471         // Metric 1 Activation 1: 100 seconds
    472         // Metric 1 Activation 2: 0 seconds
    473         // Metric 2 Activation 1: 100 seconds
    474         // Metric 2 Activation 2: 0 seconds
    475         doAppBreadcrumbReported(act1MatcherId);
    476         Thread.sleep(10L);
    477 
    478         // Fifth logged event for Metric 1.
    479         // Second logged event for Metric 2.
    480         // Fifth logged event for Metric 3.
    481         logAllMetrics();
    482 
    483         // Expire all activations.
    484         // Time remaining:
    485         // Metric 1 Activation 1: 0 seconds
    486         // Metric 1 Activation 2: 0 seconds
    487         // Metric 2 Activation 1: 0 seconds
    488         // Metric 2 Activation 2: 0 seconds
    489         Thread.sleep(act1TtlSecs * 1000L);
    490 
    491         // Metric 1 event ignored.
    492         // Metric 2 event ignored.
    493         // Sixth logged event for Metric 3.
    494         logAllMetrics();
    495 
    496         // Time remaining:
    497         // Metric 1 Activation 1: 0 seconds
    498         // Metric 1 Activation 2: 0 seconds
    499         // Metric 2 Activation 1: 0 seconds
    500         // Metric 2 Activation 2: 0 seconds
    501         rebootDeviceAndWaitUntilReady();
    502 
    503         // Metric 1 event ignored.
    504         // Metric 2 event ignored.
    505         // Seventh logged event for Metric 3.
    506         logAllMetrics();
    507 
    508         ConfigMetricsReportList reportList = getReportList();
    509         assertEquals(3, reportList.getReportsCount());
    510 
    511         // Report before restart.
    512         ConfigMetricsReport report = reportList.getReports(0);
    513         verifyMetrics(report, 3, 0, 3);
    514 
    515         // Report after first restart.
    516         report = reportList.getReports(1);
    517         verifyMetrics(report, 2, 2, 3);
    518 
    519         // Report after second restart.
    520         report = reportList.getReports(2);
    521         verifyMetrics(report, 0, 0, 1);
    522     }
    523 
    524     private void logAllMetrics() throws Exception {
    525         doAppBreadcrumbReported(metric1MatcherId);
    526         Thread.sleep(10L);
    527 
    528         doAppBreadcrumbReported(metric2MatcherId);
    529         Thread.sleep(10L);
    530 
    531         doAppBreadcrumbReported(metric3MatcherId);
    532         Thread.sleep(10L);
    533     }
    534 
    535     private void verifyMetrics(ConfigMetricsReport report, int metric1Count, int metric2Count,
    536             int metric3Count) throws Exception {
    537         assertEquals(3, report.getMetricsCount());
    538 
    539         verifyMetric(
    540                 report.getMetrics(0),   // StatsLogReport
    541                 1,                      // Metric Id
    542                 1,                      // Metric what atom matcher label
    543                 metric1Count            // Data count
    544         );
    545         verifyMetric(
    546                 report.getMetrics(1),   // StatsLogReport
    547                 2,                      // Metric Id
    548                 2,                      // Metric what atom matcher label
    549                 metric2Count            // Data count
    550         );
    551         verifyMetric(
    552                 report.getMetrics(2),   // StatsLogReport
    553                 3,                      // Metric Id
    554                 3,                      // Metric what atom matcher label
    555                 metric3Count            // Data count
    556         );
    557     }
    558 
    559     private void verifyMetric(StatsLogReport metricReport, long metricId, int metricMatcherLabel,
    560             int dataCount) {
    561         LogUtil.CLog.d("Got the following event metric data: " + metricReport.toString());
    562         assertEquals(metricId, metricReport.getMetricId());
    563         if (dataCount > 0) {
    564             assertTrue(metricReport.hasEventMetrics());
    565         } else {
    566             assertFalse(metricReport.hasEventMetrics());
    567         }
    568         StatsLogReport.EventMetricDataWrapper eventData = metricReport.getEventMetrics();
    569         assertEquals(dataCount, eventData.getDataCount());
    570         for (int i = 0; i < eventData.getDataCount(); i++) {
    571             AppBreadcrumbReported atom = eventData.getData(i).getAtom().getAppBreadcrumbReported();
    572             assertEquals(metricMatcherLabel, atom.getLabel());
    573         }
    574     }
    575 }
    576