Files
LocalAI/core/http/endpoints/localai/system.go
T
localai-org-maint-botandlocalai-org-maint-bot f154bd990a feat(system): report per-model DRM VRAM (#12026)
* feat(system): report per-model DRM VRAM

Expose optional resident device memory for local backend process trees.
Deduplicate DRM clients and omit unsupported or incomplete readings.
Document accounting limits and preserve a measured zero in JSON.

Closes #11970.

Assisted-by: Codex:gpt-6

* fix(system): document trusted procfs reads

Scope G304 annotations to paths built from the fixed procfs root,
integer process IDs, and kernel directory entries. These reads accept
no user-controlled path components.

Assisted-by: Codex:GPT-6 gosec

---------

Co-authored-by: localai-org-maint-bot <306269227+localai-org-maint-bot@users.noreply.github.com>
2026-09-27 21:18:34 +02:00

78 lines
2.3 KiB
Go

package localai
import (
"strconv"
"github.com/labstack/echo/v4"
"github.com/mudler/LocalAI/core/config"
"github.com/mudler/LocalAI/core/schema"
"github.com/mudler/LocalAI/core/services/monitoring"
"github.com/mudler/LocalAI/pkg/model"
"github.com/mudler/LocalAI/pkg/xsysinfo"
)
// SystemInformations returns the system informations
// @Summary Show the LocalAI instance information
// @Tags monitoring
// @Success 200 {object} schema.SystemInformationResponse "Response"
// @Router /system [get]
func SystemInformations(cl *config.ModelConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig, sampler *monitoring.LocalProcessSampler) echo.HandlerFunc {
return func(c echo.Context) error {
availableBackends := []string{}
loadedModels := ml.ListLoadedModels()
for b := range appConfig.ExternalGRPCBackends {
availableBackends = append(availableBackends, b)
}
for b := range ml.GetAllExternalBackends(nil) {
availableBackends = append(availableBackends, b)
}
sysmodels := []schema.SysInfoModel{}
live := map[int32]struct{}{}
for _, m := range loadedModels {
entry := schema.SysInfoModel{ID: m.ID}
// The loader tracks only the ID. Which engine is serving a model is
// the first thing an operator wants beside its name, and it is one
// config lookup away.
if cfg, ok := cl.GetModelConfig(m.ID); ok {
entry.Backend = cfg.Backend
}
if pid, ok := localPID(m); ok && sampler != nil {
live[pid] = struct{}{}
if proc, err := sampler.Sample(pid); err == nil {
entry.Process = proc
}
}
if pid, ok := localPID(m); ok {
if used, ok := xsysinfo.ProcessVRAM(int(pid)); ok {
entry.SizeVRAM = &used
}
}
sysmodels = append(sysmodels, entry)
}
if sampler != nil {
sampler.Retain(live)
}
return c.JSON(200,
schema.SystemInformationResponse{
Backends: availableBackends,
Models: sysmodels,
},
)
}
}
// localPID is the PID of the backend process this host started for m. A model
// served by a remote worker, or through an external gRPC address, has none.
func localPID(m *model.Model) (int32, bool) {
p := m.Process()
if p == nil {
return 0, false
}
pid, err := strconv.ParseInt(p.CurrentPID(), 10, 32)
if err != nil || pid <= 0 {
return 0, false
}
return int32(pid), true
}