//! Bounded Ready queue balancing with an owned per-thread affinity policy. //! A sampled load is inert evidence. Actual source and target queues validate //! the handoff again before the owned migration ticket can be adopted. use crate::runtime::{percpu_queue_context as context,percpu_queue_local::{Binding,Local,OnlineOwner},thread_migration::Ticket,percpu_queue::{Backend,Handle,Observation,Record,Snapshot},percpu_queue_threads::{ThreadId,State}}; #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Error { Mask,Thread,Context,Stale,NoTarget,Load,Ticket,Exhausted } #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Load{binding:Binding,snapshot:Snapshot,order:[Option;N],root:u64} impl Load{ pub fn observe(q:&Local)->Result{ let snapshot=q.snapshot().map_err(|_|Error::Context)?; let binding=q.binding(); if N==0||N>32||!(1..=3).contains(&binding.cpu)||!binding.online||binding.generation==0||binding.issuer==0||binding.epoch==0 ||snapshot.members>N||snapshot.queued>snapshot.members||snapshot.active!=0 {return Err(Error::Context)} let order=q.ready_order().map_err(|_|Error::Context)?; let root=q.with_backend_observation(|b|b.execution().root).map_err(|_|Error::Context)?; if root==0||root%4096!=0||root>>48!=0{return Err(Error::Context)} Ok(Self{binding,snapshot,order,root}) } pub fn cpu(&self)->u32{self.binding.cpu} pub fn ready(&self)->usize{self.snapshot.queued} pub fn binding(&self)->Binding{self.binding} } pub struct Affinity{thread:ThreadId,allowed:u64,revision:u64} fn mask(cpu:u32,allowed:u64)->Result<(),Error>{if !(1..=3).contains(&cpu)||allowed&!0b1110!=0||allowed&(1<(q:&Local,h:Handle)->Result<(Record,Observation),Error>{ let r=q.record(h).map_err(|_|Error::Thread)?; let o=q.observe_member(h).map_err(|_|Error::Thread)?; if !r.queued||r.active||r.thread!=o.thread.id||r.slice!=0||r.slices!=0||r.consumed_ticks!=0||r.remaining!=0 ||o.thread.state!=State::Ready||o.context.state!=context::State::Ready||o.context.last_capture!=0 ||!matches!(o.context.image,context::Image::Initial{..}){return Err(Error::Thread)} Ok((r,o)) } impl Affinity{ /// # Safety /// The kernel task owner issues exactly one policy for this live thread. /// No other policy or balance plan for the same thread may coexist, and /// `allowed` is authorized by that owner. Policy updates use this value. pub unsafe fn new(q:&Local,h:Handle,allowed:u64)->Result{ let(r,_)=observed(q,h)?;mask(q.binding().cpu,allowed)?;Ok(Self{thread:r.thread,allowed,revision:1}) } pub fn thread(&self)->ThreadId{self.thread} pub fn allowed(&self)->u64{self.allowed} pub fn revision(&self)->u64{self.revision} pub fn update(&mut self,q:&Local,h:Handle,allowed:u64)->Result<(),Error>{ let(r,_)=observed(q,h)?;if r.thread!=self.thread{return Err(Error::Thread)}mask(q.binding().cpu,allowed)?; let revision=self.revision.checked_add(1).ok_or(Error::Exhausted)?;self.allowed=allowed;self.revision=revision;Ok(()) } pub fn plan(self,q:&Local,h:Handle,targets:&[Load])->Result,(Error,Self)>{ let result=(||{ let source=Load::observe(q)?;let(r,o)=observed(q,h)?;if r.thread!=self.thread{return Err(Error::Thread)}mask(source.cpu(),self.allowed)?;self.revision.checked_add(1).ok_or(Error::Exhausted)?;if h.generation()==u64::MAX{return Err(Error::Exhausted)} let mut seen=0u64;let mut selected:Option>=None; for target in targets{ let bit=1u64.checked_shl(target.cpu()).ok_or(Error::Context)?; if seen&bit!=0||target.binding.issuer!=source.binding.issuer||target.binding.epoch!=source.binding.epoch||target.root!=source.root{return Err(Error::Context)}seen|=bit; if target.cpu()==source.cpu()||self.allowed&bit==0||target.snapshot.members+target.snapshot.retired>=N||source.ready()Ok(Plan{policy:self,source,target,record,observation}),Err(e)=>Err((e,self))} } } pub struct Plan{policy:Affinity,source:Load,target:Load,record:Record,observation:Observation} impl Plan{ pub fn target(&self)->Binding{self.target.binding} pub fn source(&self)->Binding{self.source.binding} pub fn thread(&self)->ThreadId{self.policy.thread} pub fn check_source(&self,q:&Local)->Result<(),Error>{ if Load::observe(q)?!=self.source{return Err(Error::Stale)}let(r,o)=observed(q,self.record.handle)?; if r!=self.record||o!=self.observation{return Err(Error::Stale)}Ok(()) } pub fn cancel(self)->Affinity{self.policy} pub fn exported(self,q:&Local,ticket:Ticket)->Result,(Error,Self,Ticket)>{ let result=(||{ let actual=Load::observe(q)?;let mut expected=self.source; let index=expected.order.iter().position(|h|*h==Some(self.record.handle)).ok_or(Error::Stale)?; for i in index+1..N{expected.order[i-1]=expected.order[i]}expected.order[N-1]=None; expected.snapshot.members=expected.snapshot.members.checked_sub(1).ok_or(Error::Load)?; expected.snapshot.queued=expected.snapshot.queued.checked_sub(1).ok_or(Error::Load)?; if self.record.handle.generation()==u64::MAX{expected.snapshot.retired+=1;} if actual!=expected||q.record(self.record.handle).is_ok(){return Err(Error::Stale)} if ticket.route()!=(self.source.binding,self.target.binding)||ticket.record()!=self.observation.thread{return Err(Error::Ticket)} Ok(()) })(); match result{Ok(())=>Ok(Flight{plan:self,ticket}),Err(e)=>Err((e,self,ticket))} } } pub struct Flight{plan:Plan,ticket:Ticket} impl Flight{ pub fn admit(self,q:&Local)->Result<(Ticket,Arrival),(Error,Self)>{ match Load::observe(q){Ok(actual)if actual==self.plan.target=>Ok((self.ticket,Arrival{plan:self.plan})),Ok(_)=>Err((Error::Stale,self)),Err(e)=>Err((e,self))} } pub fn into_recall(self)->(Ticket,Recall){(self.ticket,Recall{plan:self.plan})} } pub struct Recall{plan:Plan} impl Recall{ pub fn complete(self,q:&Local,h:Handle)->Result{ let result=(||{ let actual=Load::observe(q)?;let(r,o)=observed(q,h)?;let mut expected=self.plan.source; let index=expected.order.iter().position(|h|*h==Some(self.plan.record.handle)).ok_or(Error::Stale)?; for i in index+1..N{expected.order[i-1]=expected.order[i]}expected.order[expected.snapshot.queued-1]=Some(h); if actual!=expected||r.class!=self.plan.record.class||r.thread==self.plan.policy.thread{return Err(Error::Stale)} let original=self.plan.observation; if o.thread.layout!=original.thread.layout||o.thread.entry!=original.thread.entry||o.thread.argument!=original.thread.argument||o.thread.wait_epoch!=0 ||o.context.image!=original.context.image||o.context.root!=original.context.root||o.stack!=original.stack{return Err(Error::Ticket)} let revision=self.plan.policy.revision.checked_add(1).ok_or(Error::Exhausted)?;Ok((r.thread,revision)) })(); match result{Ok((thread,revision))=>Ok(Affinity{thread,revision,allowed:self.plan.policy.allowed}),Err(e)=>Err((e,self))} } } pub struct Arrival{plan:Plan} impl Arrival{ pub fn complete(self,q:&Local,h:Handle)->Result{ let result=(||{ let actual=Load::observe(q)?;let(r,o)=observed(q,h)?;let mut expected=self.plan.target; if expected.snapshot.queued>=N||expected.snapshot.members>=N{return Err(Error::Load)} expected.order[expected.snapshot.queued]=Some(h);expected.snapshot.members+=1;expected.snapshot.queued+=1; if actual!=expected||r.class!=self.plan.record.class||r.thread==self.plan.policy.thread{return Err(Error::Stale)} let original=self.plan.observation; if o.thread.layout!=original.thread.layout||o.thread.entry!=original.thread.entry||o.thread.argument!=original.thread.argument||o.thread.wait_epoch!=0 ||o.context.image!=original.context.image||o.context.root!=original.context.root||o.stack!=original.stack{return Err(Error::Ticket)} let revision=self.plan.policy.revision.checked_add(1).ok_or(Error::Exhausted)?;Ok((r.thread,revision)) })(); match result{Ok((thread,revision))=>Ok(Affinity{thread,revision,allowed:self.plan.policy.allowed}),Err(e)=>Err((e,self))} } } pub const fn os_k2060_layout()->[u64;63]{use core::mem::{size_of,offset_of};use crate::runtime::percpu_queue_threads::{ThreadRecord,StackObservation};[(size_of::())as u64,(offset_of!(Affinity,thread))as u64,(offset_of!(Affinity,allowed))as u64,(offset_of!(Affinity,revision))as u64,(size_of::>())as u64,(offset_of!(Load,binding))as u64,(offset_of!(Load,snapshot))as u64,(offset_of!(Load,order))as u64,(offset_of!(Load,root))as u64,(size_of::())as u64,(offset_of!(Snapshot,members))as u64,(offset_of!(Snapshot,queued))as u64,(offset_of!(Snapshot,active))as u64,(offset_of!(Snapshot,retired))as u64,(offset_of!(Snapshot,epoch))as u64,(offset_of!(Snapshot,consumed_ticks))as u64,(offset_of!(Snapshot,faulted))as u64,(offset_of!(Snapshot,retained_prepared))as u64,(size_of::>())as u64,(offset_of!(Plan,policy))as u64,(offset_of!(Plan,source))as u64,(offset_of!(Plan,target))as u64,(offset_of!(Plan,record))as u64,(offset_of!(Plan,observation))as u64,(size_of::>())as u64,(offset_of!(Flight,plan))as u64,(offset_of!(Flight,ticket))as u64,(size_of::>())as u64,(offset_of!(Arrival,plan))as u64,(size_of::())as u64,(offset_of!(Record,handle))as u64,(offset_of!(Record,thread))as u64,(offset_of!(Record,class))as u64,(offset_of!(Record,queued))as u64,(offset_of!(Record,active))as u64,(offset_of!(Record,remaining))as u64,(offset_of!(Record,slice))as u64,(offset_of!(Record,consumed_ticks))as u64,(offset_of!(Record,slices))as u64,(size_of::())as u64,(offset_of!(Observation,thread))as u64,(offset_of!(Observation,context))as u64,(offset_of!(Observation,stack))as u64,(size_of::())as u64,(offset_of!(context::Record,handle))as u64,(offset_of!(context::Record,thread))as u64,(offset_of!(context::Record,state))as u64,(offset_of!(context::Record,root))as u64,(offset_of!(context::Record,image))as u64,(offset_of!(context::Record,last_capture))as u64,(size_of::())as u64,(offset_of!(ThreadRecord,id))as u64,(offset_of!(ThreadRecord,state))as u64,(offset_of!(ThreadRecord,layout))as u64,(offset_of!(ThreadRecord,entry))as u64,(offset_of!(ThreadRecord,argument))as u64,(offset_of!(ThreadRecord,wait_epoch))as u64,(size_of::())as u64,(offset_of!(StackObservation,layout))as u64,(offset_of!(StackObservation,guard_descriptor))as u64,(offset_of!(StackObservation,low_canary))as u64,(offset_of!(StackObservation,high_canary))as u64,(offset_of!(StackObservation,initially_zeroed))as u64] }