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

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

import com.xyzshell.andinfo.libs.cpu.models.Cache
import com.xyzshell.andinfo.libs.cpu.models.Cluster
import com.xyzshell.andinfo.libs.cpu.models.Core
import com.xyzshell.andinfo.libs.cpu.models.Package
import com.xyzshell.andinfo.libs.cpu.models.ProcessorCache

/**
 * `struct cpuinfo_processor`
 * 逻辑处理器信息结构体
 */
data class Processor(
    /**
     * SMT (hyperthread) ID within a core
     * 核心内的SMT（超线程）ID
     */
    val smtId: UInt,

    /**
     * Core containing this logical processor
     * 包含此逻辑处理器的核心
     */
    val core: Core,

    /**
     * Cluster of cores containing this logical processor
     * 包含此逻辑处理器的核心集群
     */
    val cluster: Cluster,

    /**
     * Physical package containing this logical processor
     * 包含此逻辑处理器的物理封装
     */
    val cpuPackage: Package,

    /**
     * Linux-specific ID for the logical processor:
     * Linux特定的逻辑处理器ID：
     * - Linux kernel exposes information about this logical processor in
     * - Linux内核在以下路径公开此逻辑处理器的信息
     * /sys/devices/system/cpu/cpu<linux_id>/
     * - Bit <linux_id> in the cpu_set_t identifies this logical processor
     * - cpu_set_t中的位<linux_id>标识此逻辑处理器
     */
    val linuxId: UInt,

    /**
     * x86 only
     * 仅x86架构
     *
     * APIC ID (unique x86-specific ID of the logical processor)
     * APIC ID（逻辑处理器的唯一x86特定ID）
     */
    val apicId: UInt?,

    /**
     * @see ProcessorCache
     * 处理器缓存信息
     */
    val cache: ProcessorCache,
) {
    companion object {
        @JvmStatic
        fun fromCpuInfo(
            smtId: Int,
            core: Core,
            cluster: Cluster,
            cpuPackage: Package,
            linuxId: Int,
            apicId: Int,
            cache: ProcessorCache,
        ) = Processor(
            smtId.toUInt(),
            core,
            cluster,
            cpuPackage,
            linuxId.toUInt(),
            apicId.takeUnless { it == 0 }?.toUInt(),
            cache,
        )
    }
}
