/*
 * SPDX-FileCopyrightText: Sebastiano Barezzi
 * SPDX-License-Identifier: Apache-2.0
 */

package com.xyzshell.andinfo.libs.cpu.models

/**
 * `struct cpuinfo_cache`
 * CPU缓存结构体
 */
data class Cache(
    /**
     * Cache size in bytes
     * 缓存大小（字节）
     */
    val size: UInt,

    /**
     * Number of ways of associativity
     * 缓存关联路数
     */
    val associativity: UInt,

    /**
     * Number of sets
     * 缓存组数
     */
    val sets: UInt,

    /**
     * Number of partitions
     * 分区数量
     */
    val partitions: UInt,

    /**
     * Line size in bytes
     * 缓存行大小（字节）
     */
    val lineSize: UInt,

    /**
     * Binary characteristics of the cache (unified cache, inclusive cache,
     * cache with complex indexing).
     * 缓存的二进制特性标志（统一缓存、包容缓存、复杂索引缓存）
     *
     * @see `CPUINFO_CACHE_UNIFIED`, `CPUINFO_CACHE_INCLUSIVE`,
     *      `CPUINFO_CACHE_COMPLEX_INDEXING`
     */
    val flags: UInt,

    /**
     * Index of the first logical processor that shares this cache
     * 共享此缓存的第一个逻辑处理器的索引
     */
    val processorStart: UInt,

    /**
     * Number of logical processors that share this cache
     * 共享此缓存的逻辑处理器数量
     */
    val processorCount: UInt,
) {
    companion object {
        @JvmStatic
        fun fromCpuInfo(
            size: Int,
            associativity: Int,
            sets: Int,
            partitions: Int,
            lineSize: Int,
            flags: Int,
            processorStart: Int,
            processorCount: Int,
        ) = Cache(
            size.toUInt(),
            associativity.toUInt(),
            sets.toUInt(),
            partitions.toUInt(),
            lineSize.toUInt(),
            flags.toUInt(),
            processorStart.toUInt(),
            processorCount.toUInt(),
        )
    }
}
