/*
 * Copyright (C) 2025 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 = "proto2";

package android.os.statsd.memory;

import "frameworks/proto_logging/stats/atoms.proto";
import "frameworks/proto_logging/stats/atom_field_options.proto";
import "frameworks/proto_logging/stats/enums/memory/enums.proto";

option java_package = "com.android.os.memory";
option java_multiple_files = true;

extend Atom {
    optional ZramMaintenanceExecuted zram_maintenance_executed = 1015 [(module) = "framework"];
    optional ZramSetupExecuted zram_setup_executed = 1029 [(module) = "framework"];

    optional ZramMmStatMmd zram_mm_stat_mmd = 10232 [(module) = "framework"];
    optional ZramBdStatMmd zram_bd_stat_mmd = 10233 [(module) = "framework"];
    optional ZramIoStatMmd zram_io_stat_mmd = 10240 [(module) = "framework"];
}

/**
 * Logged when zram maintenance is executed in mmd.
 *
 * Logged from:
 *   * system/memory/mmd
 *
 * Estimated Logging Rate:
 *   ZramMaintenance is triggered at most once per 1 hour
 *
 * Next Tag: 13
 */
message ZramMaintenanceExecuted {
    optional android.memory.ZramWritebackResult writeback_result = 1;
    optional int64 writeback_huge_idle_pages = 2;
    optional int64 writeback_idle_pages = 3;
    optional int64 writeback_huge_pages = 4;
    optional int64 writeback_latency_millis = 5;
    optional int64 writeback_limit_kb = 6;
    optional int64 writeback_daily_limit_kb = 7;
    optional int64 writeback_actual_limit_kb = 8;
    optional int64 writeback_total_kb = 9;

    optional android.memory.ZramRecompressionResult recompression_result = 10;
    optional int64 recompress_latency_millis = 11;

    optional int64 interval_from_previous_seconds = 12;
}


/**
 * Zram stats from /sys/block/zram0/mm_stat
 *
 * Logged from:
 *   * system/memory/mmd
 *
 * Next Tag: 11
 */
message ZramMmStatMmd {
    // Uncompressed size of data stored in this disk. This excludes
    // same-element-filled pages (same_pages) since no memory is allocated for
    // them.
    optional int64 orig_data_kb = 1;
    // Compressed size of data stored in this disk.
    optional int64 compr_data_kb = 2;
    // The amount of memory allocated for this disk. This includes allocator
    // fragmentation and metadata overhead, allocated for this disk. So,
    // allocator space efficiency can be calculated using compr_data_size and
    // this statistic.
    optional int64 mem_used_total_kb = 3;
    // The maximum amount of memory ZRAM can use to store The compressed data.
    optional int64 mem_limit_kb = 4;
    // The maximum amount of memory zram have consumed to store the data.
    //
    // In zram_drv.h we define max_used_pages as atomic_long_t which could be
    // negative, but negative value does not make sense for the variable.
    optional int64 mem_used_max_kb = 5;
    // The number of same element filled pages written to this disk. No memory
    // is allocated for such pages.
    optional int64 same_pages_kb = 6;
    // The number of pages freed during compaction.
    optional int64 pages_compacted_kb = 7;
    // The number of incompressible pages.
    // Start supporting from v4.19.
    optional int64 huge_pages_kb = 8;
    // The number of huge pages since zram set up.
    // Start supporting from v5.15.
    optional int64 huge_pages_since_kb = 9;
    // The number of huge pages removed since zram set up.
    // This is calculated from huge_pages_since_kb and huge_pages_kb.
    optional int64 huge_pages_removed_since_kb = 10;
}

/**
 * Zram writeback stats from /sys/block/zram0/bd_stat
 *
 * Logged from:
 *   * system/memory/mmd
 *
 * Next Tag: 4
 */
message ZramBdStatMmd {
    /// Size of data written in backing device.
    optional int64 bd_count_kb = 1;
    /// The number of reads from backing device.
    optional int64 bd_reads_kb = 2;
    /// The number of writes to backing device.
    optional int64 bd_writes_kb = 3;
}

/**
 * Zram IO stats from /sys/block/zram0/io_stat
 *
 * Logged from:
 *   * system/memory/mmd
 *
 * Next Tag: 5
 */
 message ZramIoStatMmd {
    // The number of failed reads in zram.
    optional int64 failed_reads = 1;
    // The number of failed writes in zram.
    optional int64 failed_writes = 2;
    // The number of non-page-size-aligned I/O requests in zram.
    optional int64 invalid_io = 3;
    // Depending on device usage scenario it may account
    // a) the number of pages freed because of swap slot free
    // notifications or b) the number of pages freed because of
    // REQ_OP_DISCARD requests sent by bio.
    optional int64 notify_free = 4;
}

/**
 * Logged when zram setup is executed in mmd.
 *
 * Logged from:
 *   * system/memory/mmd
 *
 * Estimated Logging Rate:
 *   ZramSetup is triggered at most once per boot.
 *
 * Next Tag: 7
 */
message ZramSetupExecuted {
    optional android.memory.ZramSetupResult zram_setup_result = 1;
    optional android.memory.ZramCompAlgorithmSetupResult comp_algorithm_setup_result = 2;
    optional android.memory.ZramWritebackSetupResult writeback_setup_result = 3;
    optional android.memory.ZramRecompressionSetupResult recompression_setup_result = 4;
    optional int64 zram_size_mb = 5;
    optional int64 writeback_size_mb = 6;
}
