@@ -46,6 +46,7 @@ struct ComputeDriverInfoView {
4646struct ComputeDriverCapabilitiesView {
4747 driver_name : String ,
4848 driver_version : String ,
49+ resources : Option < openshell_core:: proto:: ResourceCapabilities > ,
4950}
5051
5152#[ derive( Debug , Clone ) ]
@@ -417,6 +418,7 @@ pub async fn gateway_info(
417418 capabilities : ComputeDriverCapabilitiesView {
418419 driver_name : capabilities. driver_name ,
419420 driver_version : capabilities. driver_version ,
421+ resources : capabilities. resource_capabilities ,
420422 } ,
421423 }
422424 } )
@@ -514,9 +516,125 @@ fn print_compute_driver_info(drivers: &[ComputeDriverInfoView]) {
514516 "Driver version:" . dimmed( ) ,
515517 driver. capabilities. driver_version
516518 ) ;
519+ if let Some ( resources) = & driver. capabilities . resources {
520+ for ( name, supported, limit, request) in [
521+ (
522+ "CPU" ,
523+ resources. cpu . as_ref ( ) . map ( |c| c. limit_supported ) ,
524+ resources
525+ . cpu
526+ . as_ref ( )
527+ . and_then ( |c| c. limit_enforcement . as_ref ( ) ) ,
528+ resources
529+ . cpu
530+ . as_ref ( )
531+ . and_then ( |c| c. request_enforcement . as_ref ( ) ) ,
532+ ) ,
533+ (
534+ "Memory" ,
535+ resources. memory . as_ref ( ) . map ( |c| c. limit_supported ) ,
536+ resources
537+ . memory
538+ . as_ref ( )
539+ . and_then ( |c| c. limit_enforcement . as_ref ( ) ) ,
540+ resources
541+ . memory
542+ . as_ref ( )
543+ . and_then ( |c| c. request_enforcement . as_ref ( ) ) ,
544+ ) ,
545+ ] {
546+ let guarantee = if supported == Some ( false ) {
547+ "unsupported" . into ( )
548+ } else {
549+ enforcement_label ( limit)
550+ } ;
551+ println ! (
552+ " {name} limits: {guarantee}; requests: {}" ,
553+ enforcement_label( request)
554+ ) ;
555+ }
556+ if let Some ( sampled) = & resources. sampled_budget {
557+ println ! (
558+ " Sampled budgets: CPU time={}, resident bytes={}, scope={}; terminate on excess={}" ,
559+ sampled. cpu_time_supported,
560+ sampled. resident_bytes_supported,
561+ scope_name( sampled. scope) ,
562+ sampled. terminate_on_excess
563+ ) ;
564+ let interval = sampled. sampling_interval . map_or_else (
565+ || "unreported" . into ( ) ,
566+ |d| format ! ( "{}.{:09} s" , d. seconds, d. nanos) ,
567+ ) ;
568+ println ! (
569+ " Sampling interval: {interval} (plus collection time; overshoot unbounded)"
570+ ) ;
571+ let resolution = sampled. cpu_time_resolution . map_or_else (
572+ || "unreported" . into ( ) ,
573+ |d| format ! ( "{}.{:09} s" , d. seconds, d. nanos) ,
574+ ) ;
575+ println ! (
576+ " CPU-time resolution: {resolution}; per-process CPU limit: {}" ,
577+ sampled. per_process_cpu_limit
578+ ) ;
579+ }
580+ } else {
581+ println ! ( " Resource guarantees: unreported" ) ;
582+ }
583+ }
584+ }
585+
586+ fn scope_name ( scope : i32 ) -> & ' static str {
587+ use openshell_core:: proto:: sandbox:: v1:: ResourceEnforcementScope as Scope ;
588+ match Scope :: try_from ( scope) {
589+ Ok ( Scope :: Process ) => "process" ,
590+ Ok ( Scope :: Container ) => "container" ,
591+ Ok ( Scope :: SandboxAccount ) => "sandbox account" ,
592+ Ok ( Scope :: VirtualMachine ) => "virtual machine" ,
593+ _ => "unreported" ,
517594 }
518595}
519596
597+ fn enforcement_label (
598+ enforcement : Option < & openshell_core:: proto:: sandbox:: v1:: ResourceEnforcement > ,
599+ ) -> String {
600+ use openshell_core:: proto:: sandbox:: v1:: ResourceEnforcementKind as Kind ;
601+ let Some ( enforcement) = enforcement else {
602+ return "unreported" . into ( ) ;
603+ } ;
604+ let kind = match Kind :: try_from ( enforcement. kind ) {
605+ Ok ( Kind :: KernelLimit ) => "kernel limit" ,
606+ Ok ( Kind :: SchedulerRequest ) => "scheduler request" ,
607+ Ok ( Kind :: Unsupported ) => "unsupported" ,
608+ _ => "unreported" ,
609+ } ;
610+ format ! ( "{kind} ({})" , scope_name( enforcement. scope) )
611+ }
612+
613+ fn resource_capabilities_json (
614+ resources : & openshell_core:: proto:: ResourceCapabilities ,
615+ ) -> serde_json:: Value {
616+ fn enforcement (
617+ e : Option < & openshell_core:: proto:: sandbox:: v1:: ResourceEnforcement > ,
618+ ) -> serde_json:: Value {
619+ e. map_or (
620+ serde_json:: Value :: Null ,
621+ |e| serde_json:: json!( { "kind" : e. kind, "scope" : e. scope} ) ,
622+ )
623+ }
624+ fn duration ( d : Option < prost_types:: Duration > ) -> serde_json:: Value {
625+ d. map_or (
626+ serde_json:: Value :: Null ,
627+ |d| serde_json:: json!( { "seconds" : d. seconds. to_string( ) , "nanos" : d. nanos} ) ,
628+ )
629+ }
630+ serde_json:: json!( {
631+ "cpu" : resources. cpu. as_ref( ) . map( |c| serde_json:: json!( { "limit_supported" : c. limit_supported, "limit_enforcement" : enforcement( c. limit_enforcement. as_ref( ) ) , "request_enforcement" : enforcement( c. request_enforcement. as_ref( ) ) } ) ) ,
632+ "memory" : resources. memory. as_ref( ) . map( |c| serde_json:: json!( { "limit_supported" : c. limit_supported, "limit_enforcement" : enforcement( c. limit_enforcement. as_ref( ) ) , "request_enforcement" : enforcement( c. request_enforcement. as_ref( ) ) } ) ) ,
633+ "gpu" : resources. gpu. as_ref( ) . map( |c| serde_json:: json!( { "default_selection_supported" : c. default_selection_supported, "count_selection_supported" : c. count_selection_supported} ) ) ,
634+ "sampled_budget" : resources. sampled_budget. as_ref( ) . map( |c| serde_json:: json!( { "cpu_time_supported" : c. cpu_time_supported, "resident_bytes_supported" : c. resident_bytes_supported, "scope" : c. scope, "sampling_interval" : duration( c. sampling_interval) , "terminate_on_excess" : c. terminate_on_excess, "cpu_time_resolution" : duration( c. cpu_time_resolution) , "per_process_cpu_limit" : c. per_process_cpu_limit} ) ) ,
635+ } )
636+ }
637+
520638fn gateway_info_to_json ( view : & GatewayInfoView ) -> serde_json:: Value {
521639 serde_json:: json!( {
522640 "gateway" : & view. gateway,
@@ -532,6 +650,7 @@ fn gateway_info_to_json(view: &GatewayInfoView) -> serde_json::Value {
532650 "capabilities" : {
533651 "driver_name" : & driver. capabilities. driver_name,
534652 "driver_version" : & driver. capabilities. driver_version,
653+ "resource_capabilities" : driver. capabilities. resources. as_ref( ) . map( resource_capabilities_json) ,
535654 } ,
536655 } ) )
537656 . collect:: <Vec <_>>( ) ,
@@ -1905,6 +2024,31 @@ mod tests {
19052024 capabilities: ComputeDriverCapabilitiesView {
19062025 driver_name: "podman" . to_string( ) ,
19072026 driver_version: "0.0.75" . to_string( ) ,
2027+ resources: Some ( openshell_core:: proto:: ResourceCapabilities {
2028+ cpu: Some ( openshell_core:: proto:: CpuResourceCapabilities {
2029+ limit_supported: true ,
2030+ limit_enforcement: Some (
2031+ openshell_core:: proto:: sandbox:: v1:: ResourceEnforcement {
2032+ kind: 2 ,
2033+ scope: 2 ,
2034+ } ,
2035+ ) ,
2036+ ..Default :: default ( )
2037+ } ) ,
2038+ sampled_budget: Some (
2039+ openshell_core:: proto:: sandbox:: v1:: SampledResourceBudgetCapabilities {
2040+ resident_bytes_supported: true ,
2041+ scope: 3 ,
2042+ sampling_interval: Some ( prost_types:: Duration {
2043+ seconds: 0 ,
2044+ nanos: 100_000_000 ,
2045+ } ) ,
2046+ terminate_on_excess: true ,
2047+ ..Default :: default ( )
2048+ } ,
2049+ ) ,
2050+ ..Default :: default ( )
2051+ } ) ,
19082052 } ,
19092053 } ] ,
19102054 extensions : vec ! [ ExtensionInfoView {
@@ -1928,6 +2072,16 @@ mod tests {
19282072 json[ "compute_drivers" ] [ 0 ] [ "capabilities" ] [ "driver_name" ] ,
19292073 "podman"
19302074 ) ;
2075+ let resources = & json[ "compute_drivers" ] [ 0 ] [ "capabilities" ] [ "resource_capabilities" ] ;
2076+ assert_eq ! (
2077+ resources[ "cpu" ] [ "limit_enforcement" ] ,
2078+ serde_json:: json!( { "kind" : 2 , "scope" : 2 } )
2079+ ) ;
2080+ assert_eq ! (
2081+ resources[ "sampled_budget" ] [ "sampling_interval" ] [ "nanos" ] ,
2082+ 100_000_000
2083+ ) ;
2084+ assert_eq ! ( resources[ "sampled_budget" ] [ "terminate_on_excess" ] , true ) ;
19312085 assert_eq ! ( json[ "extensions" ] [ 0 ] [ "protocol_version" ] , "1.0" ) ;
19322086 assert_eq ! (
19332087 json[ "compute_drivers" ] [ 0 ] [ "capabilities" ] [ "driver_version" ] ,
0 commit comments