VMM中各个类的构造函数--new的参数

本文介绍了VMM架构中各类组件的初始化过程,包括vmm_data派生类、vmm_xactor基类及其派生类的new函数实现,以及vmm_env如何自动配置log和notify服务。特别关注了stream_id、data_id的分配机制与接口绑定。

vmm_data的派生类的new函数中,只是用静态的log去初始化消息服务窗口和通知窗口。vmm_data的stream_id和transactor相关,因此stream_id由transactor给出。同理,data_id 由generator在每产生一个产品的时侯increase。

function new();
    super.new(log);
endfunction

vmm_xactor基类已经准备好了vmm_log, vmm_notify接口,无需在派生类中进行额外处理。vmm_xactor的派生类在通过new创建对象的时侯,对stream_id进行赋值,以及interface,name, instance

function new(string inst, int stream_id, chan_name in_chan=null, virtual xxif.TB if);
    super.new("xxx", inst, stream_id);
    if (in_chan == null) begin 
       in_chan = new("xxx input channel",inst}, 
    end 
    this.out_chan = out_chan; 
    this.if = if;
endfunction

 类似的,vmm_atomic_gen产生generator的new函数如下

new(string inst, int stream_id, chan_name out_chan=null);

vmm_env自动给出了自己的log和notify。扩展类只需要在new中加上一个virtual interface

function new(virtual xxx.IF  if);   
    super.new();
    this.if=if;
endfunction 
import lombok.Data; //lombok依赖 import org.cloudbus.cloudsim.*; import org.cloudbus.cloudsim.core.CloudSim; import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple; import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple; import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple; import org.cloudbus.cloudsim.Vm; import java.text.DecimalFormat; import java.util.*; import java.util.stream.Collectors; @Data //这个注解主要就是在编译时自动生成属性的get和set方法 public class Instance { private Integer vmId=0; private Integer cloudletId=0; private List<Host> hostList=new ArrayList<>(); private List<Pe> peList=new ArrayList<>(); private List<Vm> vmlist=new ArrayList<>(); private List<Cloudlet> cloudletList=new ArrayList<>(); private List<DatacenterBroker> brokerList=new ArrayList<>() ; private List<Datacenter> datacenterList=new ArrayList<>(); public void initHost(Integer hostId) { hostList = new ArrayList<Host>(); peList=new ArrayList<>(); int mips=3000; peList.add(new Pe(0, new PeProvisionerSimple(mips))); peList.add(new Pe(0, new PeProvisionerSimple(mips))); int ram = 2048; long storage = 1000000; int bw = 4000; hostList.add( new Host( hostId, new RamProvisionerSimple(ram), new BwProvisionerSimple(bw), storage, peList, new VmSchedulerSpaceShared(peList) ) ); } public Datacenter initDatacenter(String name,Integer hostId) { initHost(hostId); String arch = "x86"; String os = "Linux"; String vmm = "Xen"; double time_zone = 10.0; double cost = 3.0; double costPerMem = 0.05; double costPerStorage = 0.001; double costPerBw = 0.0; LinkedList<Storage> storageList = new LinkedList<Storage>(); DatacenterCharacteristics characteristics = new DatacenterCharacteristics( arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw); Datacenter datacenter = null; try { datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0); datacenterList.add(datacenter); System.out.println(name+"创建成功"); return datacenter; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("DataCenter创建失败"); } } public void initVm(Integer num,Integer brokerIndex) { int mips = 1000; long size = 10000; int ram = 512; long bw = 1000; int pesNumber = 1; String vmm = "Xen"; try { for(int i=0;i<num;i++) { Vm vm = new Vm(vmId, brokerList.get(brokerIndex).getId(), mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared()); vmId++; vmlist.add(vm); } List<Vm> collect = vmlist.stream().filter((vm) -> vm.getUserId() == brokerList.get(brokerIndex).getId()).collect(Collectors.toList()); brokerList.get(brokerIndex).submitVmList(collect); }catch (Exception e){ e.printStackTrace(); } } public void initCloudlet(Integer start,Integer end,Integer brokerIndex) { long length = 400000; long fileSize = 300; long outputSize = 300; UtilizationModel utilizationModel = new UtilizationModelFull(); for(int i=start;i<=end;i++) { Cloudlet cloudlet = new Cloudlet(cloudletId, length, 1, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel); cloudlet.setUserId(brokerList.get(brokerIndex).getId()); cloudlet.setVmId(vmlist.get(i).getId()); cloudletId++; cloudletList.add(cloudlet); } List<Cloudlet> collect = cloudletList.stream().filter((cloudlet) -> cloudlet.getUserId() == brokerList.get(brokerIndex).getId()).collect(Collectors.toList()); brokerList.get(brokerIndex).submitCloudletList(collect); } public static void printCloudletList(List<Cloudlet> list) { int size = list.size(); Cloudlet cloudlet; String indent = " "; Log.printLine(); Log.printLine("========== OUTPUT =========="); Log.printLine("Cloudlet ID" + indent + "STATUS" + indent + "Data center ID" + indent + "VM ID" + indent + "Time" + indent + "Start Time" + indent + "Finish Time"); DecimalFormat dft = new DecimalFormat("###.##"); for (int i = 0; i < size; i++) { cloudlet = list.get(i); Log.print(indent + cloudlet.getCloudletId() + indent + indent); if (cloudlet.getStatus() == Cloudlet.SUCCESS) { Log.print("SUCCESS"); Log.printLine(indent + indent + cloudlet.getResourceId() + indent + indent + indent + cloudlet.getVmId() + indent + indent + dft.format(cloudlet.getActualCPUTime()) + indent + indent + dft.format(cloudlet.getExecStartTime()) + indent + indent + dft.format(cloudlet.getFinishTime())); } } public static void main(String[] args) throws CloneNotSupportedException { int numUser = 7; Calendar calendar = Calendar.getInstance(); boolean traceFlag = false; CloudSim.init(numUser, calendar, traceFlag); Instance instance=new Instance(); try{ Datacenter datacenter = instance.initDatacenter("Datacenter_1", 0); Datacenter datacenter1 = instance.initDatacenter("Datacenter_2", 0); Datacenter datacenter2 = instance.initDatacenter("Datacenter_3", 1); Datacenter datacenter3 = instance.initDatacenter("Datacenter_4", 1); DatacenterBroker broker1=new DatacenterBroker("1"); DatacenterBroker broker2=new DatacenterBroker("2"); DatacenterBroker broker3=new DatacenterBroker("3"); DatacenterBroker broker4=new DatacenterBroker("4"); instance.getBrokerList().add(broker1); instance.getBrokerList().add(broker2); instance.getBrokerList().add(broker3); instance.getBrokerList().add(broker4); instance.initVm(2,0); instance.initVm(2,1); instance.initVm(2,2); instance.initVm(2,3); instance.initCloudlet(0,1,0); instance.initCloudlet(2,3,1); instance.initCloudlet(4,5,2); instance.initCloudlet(6,7,3); CloudSim.startSimulation(); CloudSim.stopSimulation(); List<Cloudlet> list = instance.getBrokerList().get(0).getCloudletReceivedList(); printCloudletList(list); List<Cloudlet> list2 = instance.getBrokerList().get(1).getCloudletReceivedList(); printCloudletList(list2); List<Cloudlet> list3 = instance.getBrokerList().get(2).getCloudletReceivedList(); printCloudletList(list3); List<Cloudlet> list4 = instance.getBrokerList().get(3).getCloudletReceivedList(); printCloudletList(list4); List<Cloudlet> total=new ArrayList<Cloudlet>(){{ addAll(list);addAll(list2);addAll(list3);addAll(list4);}}; printCloudletList(total); }catch (Exception e) { e.printStackTrace(); System.out.println("初始化失败"); } } }
05-14
pub(crate) fn new_internal( // connection: vmm_vhost::Connection<vmm_vhost::FrontendReq>, device_type: DeviceType, max_queue_size: Option<u16>, mut base_features: u64, cfg: Option<&[u8]>, pci_address: Option<PciAddress>, ) -> Result<VhostUserFrontend>{ // Don't allow packed queues even if requested. We don't handle them properly yet at the // protocol layer. // TODO: b/331466964 - Remove once packed queue support is added to BackendClient. if base_features & (1 << virtio_sys::virtio_config::VIRTIO_F_RING_PACKED) != 0 { base_features &= !(1 << virtio_sys::virtio_config::VIRTIO_F_RING_PACKED); base::warn!( "VIRTIO_F_RING_PACKED requested, but not yet supported by vhost-user frontend. \ Automatically disabled." ); } #[cfg(windows)] let backend_pid = connection.target_pid(); let mut backend_client = BackendClient::new(connection); backend_client.set_owner().map_err(Error::SetOwner)?; let allow_features = VIRTIO_DEVICE_TYPE_SPECIFIC_FEATURES_MASK | base_features | 1 << VHOST_USER_F_PROTOCOL_FEATURES; let avail_features = allow_features & backend_client.get_features().map_err(Error::GetFeatures)?; let mut acked_features = 0; let mut allow_protocol_features = VhostUserProtocolFeatures::CONFIG | VhostUserProtocolFeatures::MQ | VhostUserProtocolFeatures::BACKEND_REQ | VhostUserProtocolFeatures::DEVICE_STATE; // HACK: the crosvm vhost-user GPU backend supports the non-standard // VHOST_USER_PROTOCOL_FEATURE_SHARED_MEMORY_REGIONS. This should either be standardized // (and enabled for all device types) or removed. let expose_shmem_descriptors_with_viommu = if device_type == DeviceType::Gpu { allow_protocol_features |= VhostUserProtocolFeatures::SHARED_MEMORY_REGIONS; true } else { false }; let mut protocol_features = VhostUserProtocolFeatures::empty(); if avail_features & 1 << VHOST_USER_F_PROTOCOL_FEATURES != 0 { // The vhost-user backend supports VHOST_USER_F_PROTOCOL_FEATURES; enable it. backend_client .set_features(1 << VHOST_USER_F_PROTOCOL_FEATURES) .map_err(Error::SetFeatures)?; acked_features |= 1 << VHOST_USER_F_PROTOCOL_FEATURES; let avail_protocol_features = backend_client .get_protocol_features() .map_err(Error::GetProtocolFeatures)?; protocol_features = allow_protocol_features & avail_protocol_features; backend_client .set_protocol_features(protocol_features) .map_err(Error::SetProtocolFeatures)?; } // if protocol feature `VhostUserProtocolFeatures::BACKEND_REQ` is negotiated. let backend_req_handler = if protocol_features.contains(VhostUserProtocolFeatures::BACKEND_REQ) { let (handler, tx_fd) = create_backend_req_handler( BackendReqHandlerImpl::new(), #[cfg(windows)] backend_pid, )?; backend_client .set_backend_req_fd(&tx_fd) .map_err(Error::SetDeviceRequestChannel)?; Some(handler) } else { None }; // If the device supports VHOST_USER_PROTOCOL_F_MQ, use VHOST_USER_GET_QUEUE_NUM to // determine the number of queues supported. Otherwise, use the minimum number of queues // required by the spec for this device type. let num_queues = if protocol_features.contains(VhostUserProtocolFeatures::MQ) { trace!("backend supports VHOST_USER_PROTOCOL_F_MQ"); let num_queues = backend_client.get_queue_num().map_err(Error::GetQueueNum)?; trace!("VHOST_USER_GET_QUEUE_NUM returned {num_queues}"); num_queues as usize } else { trace!("backend does not support VHOST_USER_PROTOCOL_F_MQ"); device_type.min_queues() }; // Clamp the maximum queue size to the largest power of 2 <= max_queue_size. let max_queue_size = max_queue_size .and_then(power_of_two_le) .unwrap_or(Queue::MAX_SIZE); trace!( "vhost-user {device_type} frontend with {num_queues} queues x {max_queue_size} entries\ {}", if let Some(pci_address) = pci_address { format!(" pci-address {pci_address}") } else { "".to_string() } ); let queue_sizes = vec![max_queue_size; num_queues]; Ok(VhostUserFrontend { device_type, worker_thread: None, backend_client: Arc::new(Mutex::new(backend_client)), avail_features, acked_features, protocol_features, backend_req_handler, shmem_region: RefCell::new(None), queue_sizes, cfg: cfg.map(|cfg| cfg.to_vec()), expose_shmem_descriptors_with_viommu, pci_address, sent_queues: None, }) } 只作解释
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值