//! Telemetry for the existing CPU0-mediated exclusive ownership handoff. //! Metrics do not grant, revoke or free the measured resource. The adapter must //! obtain boundaries from the real owner protocol and keep those raw receipts. #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Error { Configuration=1, MissingBoundary, Context, Stale, Sequence, Clock, Contention, Exhausted } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Binding { pub issuer:u64, pub epoch:u64, pub generation:u64, pub cpu:u64 } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Sample { pub binding:Binding, pub sequence:u64, pub contended:u64, pub polls:u64, pub requested:u64, pub acquired:u64, pub released:u64 } #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Measurement { Split(Sample), Combined{cpu:u64,total:u64} } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Distribution { pub count:u64, pub minimum:u64, pub maximum:u64, pub total:u64 } impl Distribution { const fn new()->Self { Self{count:0,minimum:0,maximum:0,total:0} } fn add(self,value:u64)->Result { Ok(Self{count:self.count.checked_add(1).ok_or(Error::Exhausted)?,minimum:if self.count==0{value}else{self.minimum.min(value)},maximum:self.maximum.max(value),total:self.total.checked_add(value).ok_or(Error::Exhausted)?}) } } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Pair { pub wait:Distribution, pub hold:Distribution } #[repr(C)] pub struct Meter { root:u64,frequency:u64,bindings:[Binding;3],sequences:[u64;3],last_release:[u64;3],groups:[Pair;6] } impl Meter { /// Caller pins these three online CPU generations for the measurement session. /// The returned object is telemetry only; it never holds resource authority. pub unsafe fn bind(root:u64,frequency:u64,bindings:[Binding;3])->Result { if root==0||root&4095!=0||root>>48!=0||frequency==0||bindings.iter().enumerate().any(|(i,b)|b.cpu!=i as u64+1||b.issuer==0||b.epoch==0||b.generation==0||b.issuer!=bindings[0].issuer||b.epoch!=bindings[0].epoch){return Err(Error::Configuration)} Ok(Self{root,frequency,bindings,sequences:[0;3],last_release:[0;3],groups:[Pair{wait:Distribution::new(),hold:Distribution::new()};6]}) } pub fn frequency(&self)->u64 { self.frequency } pub fn sequences(&self)->[u64;3] { self.sequences } pub fn last_release(&self)->[u64;3] { self.last_release } pub fn groups(&self)->[Pair;6] { self.groups } pub fn record(&mut self,root:u64,input:Measurement)->Result<(),Error> { if root!=self.root{return Err(Error::Context)} let Measurement::Split(s)=input else{return Err(Error::MissingBoundary)}; if s.binding.cpu==0||s.binding.cpu>3{return Err(Error::Stale)} let cpu=s.binding.cpu as usize-1; if s.binding!=self.bindings[cpu]{return Err(Error::Stale)} if s.sequence!=self.sequences[cpu].checked_add(1).ok_or(Error::Exhausted)?||s.sequence>4{return Err(Error::Sequence)} if s.requested==0||s.requested2)||s.contended!=u64::from(s.polls!=0)||s.polls!=0&&s.acquired==s.requested{return Err(Error::Contention)} let group=cpu*2+s.contended as usize; let pair=Pair{wait:self.groups[group].wait.add(s.acquired-s.requested)?,hold:self.groups[group].hold.add(s.released-s.acquired)?}; self.groups[group]=pair;self.sequences[cpu]=s.sequence;self.last_release[cpu]=s.released;Ok(()) } } pub const fn os_k2090_layout()->[u64;28]{use core::mem::{size_of,offset_of};[(size_of::())as u64,(offset_of!(Binding,issuer))as u64,(offset_of!(Binding,epoch))as u64,(offset_of!(Binding,generation))as u64,(offset_of!(Binding,cpu))as u64,(size_of::())as u64,(offset_of!(Sample,binding))as u64,(offset_of!(Sample,sequence))as u64,(offset_of!(Sample,contended))as u64,(offset_of!(Sample,polls))as u64,(offset_of!(Sample,requested))as u64,(offset_of!(Sample,acquired))as u64,(offset_of!(Sample,released))as u64,(size_of::())as u64,(offset_of!(Distribution,count))as u64,(offset_of!(Distribution,minimum))as u64,(offset_of!(Distribution,maximum))as u64,(offset_of!(Distribution,total))as u64,(size_of::())as u64,(offset_of!(Pair,wait))as u64,(offset_of!(Pair,hold))as u64,(size_of::())as u64,(offset_of!(Meter,root))as u64,(offset_of!(Meter,frequency))as u64,(offset_of!(Meter,bindings))as u64,(offset_of!(Meter,sequences))as u64,(offset_of!(Meter,last_release))as u64,(offset_of!(Meter,groups))as u64] }