/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

syntax = "proto3";

package com.android.server.art.proto;
option java_multiple_files = true;

// Pre-reboot Dexopt metrics to persist on disk for being reported after reboot.
// This proto is persisted on disk and forward and backward compatibility are considerations.
message PreRebootStats {
    // Overall status of the job right before the reboot.
    // See `android.os.statsd.art.PreRebootDexoptJobEnded`.
    enum Status {
        STATUS_UNKNOWN = 0;
        STATUS_SCHEDULED = 1;
        STATUS_STARTED = 2;
        STATUS_FAILED = 3;
        // The job is either finished or cancelled. The server will determine the case based on the
        // package counts.
        STATUS_FINISHED = 4;
        STATUS_CANCELLED = 5 [deprecated = true];
        // System requirements are not met.
        STATUS_ABORTED_SYSTEM_REQUIREMENTS = 6;
        STATUS_NOT_SCHEDULED_DISABLED = 7;
        STATUS_NOT_SCHEDULED_JOB_SCHEDULER = 8;
    }
    optional Status status = 1;

    // Number of packages successfully optimized.
    optional int32 optimized_package_count = 2;
    // Number of packages failed to optimize.
    optional int32 failed_package_count = 3;
    // Number of packages skipped.
    optional int32 skipped_package_count = 4;
    // Total number of packages scanned.
    optional int32 total_package_count = 5;

    // When the job is scheduled, in milliseconds.
    optional int64 job_scheduled_timestamp_millis = 6;

    // Represents a job run.
    message JobRun {
        // When the job is started, in milliseconds.
        optional int64 job_started_timestamp_millis = 1;
        // When the job is ended (failed, finished, or cancelled), in milliseconds.
        optional int64 job_ended_timestamp_millis = 2;
    }

    // All job runs. The job may be cancelled and rerun multiple times.
    repeated JobRun job_runs = 7;

    // Number of packages that have Pre-reboot Dexopt artifacts before the reboot. Note that this
    // isn't necessarily equal to `optimized_package_count` because packages failed to be optimized
    // may still have some splits successfully optimized.
    optional int32 packages_with_artifacts_before_reboot_count = 8;

    // The type of the job.
    enum JobType {
        JOB_TYPE_UNKNOWN = 0;
        JOB_TYPE_OTA = 1;
        JOB_TYPE_MAINLINE = 2;
    }
    optional JobType job_type = 9;

    // The reason for the failure. Only applicable when `status` is `STATUS_FAILED`.
    enum FailureReason {
        // A catch-all value for all uncategorized failures.
        FAILURE_UNSPECIFIED = 0;
        // update_engine error.
        FAILURE_UPDATE_ENGINE = 1;
        // Failed to set up chroot.
        FAILURE_CHROOT_SETUP = 2;
        // Failed to create a class loader for the new service-art.jar.
        FAILURE_CLASS_LOADER = 3;
    }

    optional FailureReason failure_reason = 10;
}
