在CloudSim源碼中的example都是通過startSimulation()啓動仿真過程,實際上startSimulation()方法內部做了非常多的工作,此次博客將詳細分析startSimulation()方法的工作流程。
startSimulation()是CloudSim類的靜態方法,該方法在所有的實體被創建和加載之後調用,開始執行CloudSim仿真,直到所有的實體完全執行(所有的實體線程到達non-RUNNABLE狀態或者在future隊列中沒有事件)才結束。
startSimulation()方法完成的核心工作:
1.開始實體
startSimulation()----->
run()----->
startEntity()----->
sendNow()-->send()-->schedule()-->send(){.....
simEvent.e
future.addEvent(e)
}
2.執行事件
startSimulation()----->
run()----->
runClockTick()----->
defferredQueue----->1)執行事件 2)發送事件並加入futureQueue
futureQueue----->把事件加入deferredQueue中1、啓動所有在startSimulation()方法之前定義好的實體(CloudSimShutdown、CloudInformationService、Datacenter、DatacenterBroker)。核心是根據不同實體間的邏輯關係創建相關事件,並將事件添加到等待隊列(future)。比如:datacenter實體首先需要向CIS註冊,因此datacenter會創建一個向CIS請求註冊的事件,並將該事件添加到等待隊列;datacenterBroker實體需要知道數據中心特徵,因此datacenterBroker會創建一個向datacenter實體請求數據中心特徵的事件,並將該事件添加到等待隊列。在下面的源碼中會按照方法調用的深度詳細分析。
(1)startSimulation()源碼分析:
點擊(此處)摺疊或打開
- // CloudSim類的靜態方法,該方法在所有的實體被創建和加載之後調用,開始執行CloudSim仿真,直到所有的實體完全執行
- // (所有的實體線程到達non-RUNNABLE狀態或者在future隊列中沒有事件)才結束。
- publicstaticdoublestartSimulation()throwsNullPointerException{
- Log.printLine("Starting CloudSim version "+CLOUDSIM_VERSION_STRING);
- try{
- // 開始仿真至結束
- double clock =run();
- // reset all static variables
- // 仿真結束,重設所有的靜態變量
- cisId =-1;
- shutdownId =-1;
- cis =null;
- calendar=null;
- traceFlag = false;
- return clock;
- }catch(IllegalArgumentException e){
- e.printStackTrace();
- thrownewNullPointerException("CloudSim.startCloudSimulation()
:"
- +" Error - you haven't initialized CloudSim.");
- }
- }
點擊(此處)摺疊或打開
- // 開始仿真直到結束仿真
- publicstaticdoublerun(){
- if(!running){
- runStart();// 開始仿真,start所有實體
- }
- .......
- }
點擊(此處)摺疊或打開
- // 啓動仿真實體
- publicstaticvoid runStart(){
- running = true;
- // Start all the entities
- for(SimEntity ent : entities){
- Log.printLine("實體名稱:"+ent.getName());
- // 每個實體都繼承了SimEntity類的startEntity()方法,實際工作是向其他實體發送一個請求事件,
- // 並將該事件添加到future隊列中。
- ent.startEntity();
- }
- printMessage("Entities started.");
- }
點擊(此處)摺疊或打開
- // 啓動雲實體
- @Override
- publicvoidstartEntity(){
- Log.printLine(getName()+"
is starting...");
- // 通過id和數據向另一個實體發送事件,請求數據中心特徵
- schedule(getId(), 0, CloudSimTags.RESOURCE_CHARACTERISTICS_REQUEST);
- }
點擊(此處)摺疊或打開
- // 向另外一個實體發送關聯事件
- publicvoid schedule(int dest,doubledelay,int tag,Object data){
- if(!CloudSim.running()){
- return;
- }
- CloudSim.send(id, dest,delay, tag, data);
- }
點擊(此處)摺疊或打開
- // 一個實體向另一個實體發送事件
- publicstaticvoidsend(int src,int dest,doubledelay,int tag,Object data){
- // 事件發生的延時不能爲負值
- if(delay< 0){
- thrownewIllegalArgumentException("Send delay can't
be negative.");
- }
- // 創建一個仿真事件對象
- SimEvent e =new SimEvent(SimEvent.SEND, clock +delay, src, dest, tag, data);
- Log.printLine("````````````````````"+e.getTag());
- // 將創建的事件添加到等待隊列中
- future.addEvent(e);
- }
2、處理具體的仿真任務,實際是處理延時隊列以及等待隊列中的各種事件。CloudSim允許仿真突然中止、在特定時刻中止仿真、在某一時刻暫停仿真,最主要也最一般的情況是在一個時鐘心跳期間處理事件隊列中的事件。在下面的源碼中會按照方法調用的深度詳細分析。
(1)處理仿真事件
點擊(此處)摺疊或打開
- // 開始仿真直到結束仿真
- publicstaticdoublerun(){
- if(!running){
- runStart();// 開始仿真,start所有實體
- }
- while(true){
- // 運行一個時鐘心跳如果future中沒有事件了,則runClockTick()返回true,或者突然中斷,while循環終止,仿真結束。
- if(runClockTick()|| abruptTerminate){
- break;
- }
- // this block allows termination of simulation at a specific time
- // 允許在特定的時間點終止仿真。
- if(terminateAt > 0.0 && clock >= terminateAt){
- terminateSimulation();// 終止仿真
- clock = terminateAt;
- break;
- }
- // 在某一時刻(pauseAt)暫停仿真
- if(pauseAt !=-1
- &&((future.size()> 0 && clock <= pauseAt &&pauseAt <=future.iterator().next()
- .eventTime())||future.size()== 0 &&pauseAt <= clock)){
- pauseSimulation();// 設置paused爲true
- clock = pauseAt;
- }
- // 如果paused爲true,則線程休眠100毫秒,但是何時會重新設置paused爲false,終止暫定仿真呢?
- while(paused){
- try{
- Thread.sleep(100);
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- }
- ........
- }
點擊(此處)摺疊或打開
- // 運行一個時鐘滴答,首先處理延時隊列(deferred)中跟各個實體相關的事件。當延時隊列爲空時,
- // 處理等待隊列(future)中的事件,問題是deferred中的事件是何時從future中添加過來的?
- publicstaticboolean runClockTick(){
- SimEntity ent;
- boolean queue_empty;
- int entities_size = entities.size();//
實體的個數
- for(int i = 0; i < entities_size; i++){//
逐個獲取實體
- ent = entities.get(i);
- if(ent.getState()== SimEntity.RUNNABLE){//
設置實體狀態爲RUNNABLE
- // run()是各實體類繼承了SimEntity類的方法,作用是處理與各個實體相關的事件。
- ent.run();
- }
- }
- // If there are more future events then deal with them
- // 等待隊列中還有未處理的事件
- if(future.size()> 0){
- List<SimEvent> toRemove =newArrayList<SimEvent>();
- Iterator<SimEvent> it =future.iterator();
- queue_empty = false;
- SimEvent first= it.next();
- processEvent(first);//
直接處理了!事件何時添加到future隊列中呢?
- future.remove(first);
- it =future.iterator();
- // Check if next events are at same time...
- boolean trymore = it.hasNext();
- while(trymore){
- SimEvent next= it.next();
- // 檢查下一個事件的時間是否相同,相同則處理下一個事件,因爲必須在同一個時鐘心跳。
- if(next.eventTime()==first.eventTime()){
- processEvent(next);
- toRemove.add(next);
- trymore = it.hasNext();
- }else{
- trymore = false;
- }
- }
- future.removeAll(toRemove);
- }else{
- queue_empty = true;
- running = false;
- printMessage("Simulation: No more future events");
- }
- return queue_empty;
- }
點擊(此處)摺疊或打開
- //所有的實體都直接繼承了run()方法,並沒有重寫。
- publicvoidrun(){
- // evbuf如果爲null,則ev=getNextEvent(),否則ev=evbuf.
- // getNextEvent():獲取延時隊列(deferred)中第一個事件(可算找到你了)
- SimEvent ev = evbuf !=null? evbuf :getNextEvent();
-
- while(ev !=null){
- // 每個實體類都有自己的processEvent()方法,哪個實體調用了run()方法,就會調用其processEvent()方法。
- processEvent(ev);
- if(state!=RUNNABLE){
- break;
- }
- ev =getNextEvent();
- }
- evbuf =null;
- }
點擊(此處)摺疊或打開
- // DatacenterBroker重載的processEvent()方法
- publicvoidprocessEvent(SimEvent ev){
- switch(ev.getTag()){
- // Resource characteristics request
- case CloudSimTags.RESOURCE_CHARACTERISTICS_REQUEST:
- processResourceCharacteristicsRequest(ev);
- break;
- // Resource characteristics answer
- case CloudSimTags.RESOURCE_CHARACTERISTICS:
- processResourceCharacteristics(ev);
- break;
- // VM Creation answer
- case CloudSimTags.VM_CREATE_ACK:
- processVmCreate(ev);
- break;
- // A finished cloudlet returned
- case CloudSimTags.CLOUDLET_RETURN:
- processCloudletReturn(ev);
- break;
- // if the simulation finishes
- case CloudSimTags.END_OF_SIMULATION:
- shutdownEntity();
- break;
- // other unknown tags are processed by this method
- default:
- processOtherEvent(ev);
- break;
- }
- }
3、獲取仿真運行時間,仿真完成,運行結束。
點擊(此處)摺疊或打開
- // 開始仿真直到結束仿真
- publicstaticdoublerun(){
- if(!running){
- runStart();// 開始仿真,start所有實體
- }
- while(true){
- // 運行一個時鐘心跳如果future中沒有事件了,則runClockTick()返回true,或者突然中斷,while循環終止,仿真結束。
- if(runClockTick()|| abruptTerminate){
- break;
- }
- // this block allows termination of simulation at a specific time
- // 允許在特定的時間點終止仿真。
- if(terminateAt > 0.0 && clock >= terminateAt){
- terminateSimulation();// 終止仿真
- clock = terminateAt;
- break;
- }
- // 在某一時刻(pauseAt)暫停仿真
- if(pauseAt !=-1
- &&(