00001 #include "charm++.h"
00002 #include "ckIgetControl.h"
00003 #ifndef _WIN32
00004 #include <sys/time.h>
00005 #include <sys/resource.h>
00006 #endif
00007 #include <unistd.h>
00008 #include <sys/types.h>
00009
00010 IGetControlClass TheIGetControlClass;
00011 int IGET_TOTALMEMORY = 480000000;
00012
00013 #if IGET_FLOWCONTROL==0
00014 #elif CKFUTURE_IGET
00015
00016 int getAvailMemory(int grainsize);
00017
00018 int
00019 IGetControlClass::IGetControlClass::iget_request(CkIGetID fut, void *msg,
00020 int ep, CkArrayID id, CkArrayIndex idx,
00021 void(*fptr)(CkArrayID,CkArrayIndex,void*,int,int),int msgsize)
00022 {
00023 if(msgsize>0) IGET_UNITMESSAGE=msgsize;
00024 int ret_status=1, size=1;
00025 if(iget_token>=size){
00026 iget_token-=size;
00027 iget_outstanding++;
00028
00029 }
00030 else
00031 {
00032 iget_tokenqueue_enqueue(fut,msg,ep,id,idx,fptr);
00033 ret_status = 0;
00034 }
00035 return ret_status;
00036 }
00037
00038 void
00039 IGetControlClass::iget_free(int size)
00040 {
00041 iget_token+=size;
00042 iget_outstanding--;
00043
00044 iget_tokenqueue_entry e=iget_tokenqueue_dequeue();
00045 if(e!=NULL)
00046 {
00047 iget_token-=size;
00048 iget_outstanding++;
00049
00050 (e->fptr)(e->aid, e->idx, e->m, e->ep, 0);
00051 delete e;
00052 }
00053 }
00054
00055 static int getRSS();
00056
00057 void
00058 IGetControlClass::iget_updateTokenNum() {
00059 double currenttime = CmiWallTimer();
00060 if(currenttime-lastupdatetime<1)
00061 return;
00062 int totalMemUsed = getRSS();
00063 if(totalMemUsed<=0) return;
00064 int leftMem = IGET_TOTALMEMORY-totalMemUsed;
00065 int iget_token_new = (leftMem)/(int)IGET_UNITMESSAGE;
00066 if(leftMem<0 || iget_token_new<IGET_MINTOKENNUM) {lastupdatetime =
00067 currenttime;return;}
00068
00069 lastupdatetime = currenttime;
00070 iget_token_history = iget_token ;
00071 iget_token = iget_token_new - iget_outstanding;
00072 if(iget_token<0) iget_token=0;
00073
00074 printf("availMem %d, IGET_UNIT %d, out_standinging %d\n",
00075 (leftMem), IGET_UNITMESSAGE, iget_outstanding);
00076 printf("Update Token num from %d to %d\n", iget_token_history, iget_token);
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 void
00108 IGetControlClass::iget_resend(CkIGetID fut)
00109 {
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128 int
00129 getAvailMemory(int grainsize)
00130 {
00131
00132 struct rusage ru;
00133 double a = 1;
00134 int chunk = 50*1024*1024;
00135 int subchunk = 20*1024*1024;
00136 unsigned long size = chunk;
00137 char *data = NULL, *olddata = NULL;
00138 unsigned long init_pf = 0;
00139 int pagesize = CmiGetPageSize();
00140
00141
00142
00143
00144
00145
00146
00147
00148 while (1) {
00149 olddata = data;
00150 data = (char *)realloc(data, size);
00151 if (data == NULL) break;
00152 for (int i=size-chunk; i<size; i+=pagesize) data[i]=i;
00153 for (int i=0; i<size; i+=pagesize) a += data[i];
00154 getrusage(RUSAGE_SELF, &ru);
00155 if (size == chunk)
00156 init_pf = ru.ru_majflt;
00157 else
00158 if (ru.ru_majflt > init_pf) break;
00159 size += chunk;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 free(olddata);
00180 return size;
00181 }
00182
00183 extern void getAvailSysMem();
00184 void getAvailSysMem() {
00185 IGET_TOTALMEMORY = getAvailMemory(0);
00186 printf("total physical memory : %d\n", IGET_TOTALMEMORY);
00187 }
00188
00189 extern void TokenUpdatePeriodic();
00190 void TokenUpdatePeriodic()
00191 {
00192 TheIGetControlClass.iget_updateTokenNum();
00193 }
00194
00195 static int getRSS()
00196 {
00197 int ret=-1, i=0;
00198 pid_t pid;
00199 char filename[128], commands[256], retstring[128];
00200 int fd;
00201 pid = getpid();
00202 sprintf(filename,"__topmem__%d", CkMyPe());
00203 sprintf(commands, "export TERM=vt100; top -b -n 1 -p %d |grep %d | awk -F' ' '{print $6}' > %s", pid,
00204 pid, filename);
00205 system(commands);
00206 i=0;
00207 while(i<10){
00208 i++;
00209 fd = open(filename, O_RDONLY);
00210 if(fd>=0) break;
00211 else
00212 printf("fileopen %s fails, try again\n", filename);
00213 }
00214 if(fd<0){
00215 printf("fileopen %s fails, abort\n", filename); return -1;
00216 }
00217 lseek(fd, 0, SEEK_SET);
00218 i=0;
00219 while(read(fd, &retstring[i], sizeof(char))>0) i++;
00220 close(fd);
00221 ret = atoi(retstring);
00222 if(i>2){
00223 if(retstring[i-2]=='m') ret *= 1024*1024;
00224 if(retstring[i-2]=='k') ret *= 1024;
00225 }
00226
00227
00228 printf("RSS %d\n",ret);
00229 return ret;
00230 }
00231
00232 #endif
00233