00001 #define NBDEBUG 0
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "includes.h"
00024
00025 #define MAX_FILES 1000
00026
00027 static char buf[70000];
00028 extern int line_count;
00029 extern int nbio_id;
00030 static int nprocs;
00031
00032 static struct {
00033 int fd;
00034 int handle;
00035 } ftable[MAX_FILES];
00036
00037 static struct children {
00038 double bytes_in, bytes_out;
00039 int line;
00040 int done;
00041 } *children;
00042
00043 double nbio_total(void)
00044 {
00045 int i;
00046 double total = 0;
00047 for (i=0;i<nprocs;i++) {
00048 total += children[i].bytes_out + children[i].bytes_in;
00049 }
00050 return total;
00051 }
00052
00053 void nb_alarm(int ignore)
00054 {
00055 int i;
00056 int lines=0, num_clients=0;
00057 if (nbio_id != -1) return;
00058
00059 for (i=0;i<nprocs;i++) {
00060 lines += children[i].line;
00061 if (!children[i].done) num_clients++;
00062 }
00063
00064 printf("%4d %8d %.2f MB/sec\r", num_clients, lines/nprocs, 1.0e-6 * nbio_total() / end_timer());
00065
00066 signal(SIGALRM, nb_alarm);
00067 alarm(1);
00068 }
00069
00070 void nbio_shmem(int n)
00071 {
00072 nprocs = n;
00073 children = (struct children *)shm_setup(sizeof(*children) * nprocs);
00074 if (!children) {
00075 printf("Failed to setup shared memory!\n");
00076 exit(1);
00077 }
00078 }
00079
00080 #if 0
00081 static int ne_find_handle(int handle)
00082 {
00083 int i;
00084 children[nbio_id].line = line_count;
00085 for (i=0;i<MAX_FILES;i++) {
00086 if (ftable[i].handle == handle) return i;
00087 }
00088 return -1;
00089 }
00090 #endif
00091
00092 static int find_handle(int handle)
00093 {
00094 int i;
00095 children[nbio_id].line = line_count;
00096 for (i=0;i<MAX_FILES;i++) {
00097 if (ftable[i].handle == handle) return i;
00098 }
00099 printf("(%d) ERROR: handle %d was not found\n",
00100 line_count, handle);
00101 exit(1);
00102
00103 return -1;
00104 }
00105
00106
00107 static struct cli_state *c;
00108
00109 static void sigsegv(int sig)
00110 {
00111 char line[200];
00112 printf("segv at line %d\n", line_count);
00113 slprintf(line, sizeof(line), "/usr/X11R6/bin/xterm -e gdb /proc/%d/exe %d",
00114 (int)getpid(), (int)getpid());
00115 system(line);
00116 exit(1);
00117 }
00118
00119 void nb_setup(struct cli_state *cli)
00120 {
00121 signal(SIGSEGV, sigsegv);
00122 c = cli;
00123 start_timer();
00124 children[nbio_id].done = 0;
00125 }
00126
00127
00128 void nb_unlink(const char *fname)
00129 {
00130 if (!cli_unlink(c, fname)) {
00131 #if NBDEBUG
00132 printf("(%d) unlink %s failed (%s)\n",
00133 line_count, fname, cli_errstr(c));
00134 #endif
00135 }
00136 }
00137
00138
00139 void nb_createx(const char *fname,
00140 unsigned create_options, unsigned create_disposition, int handle)
00141 {
00142 int fd, i;
00143 uint32 desired_access;
00144
00145 if (create_options & FILE_DIRECTORY_FILE) {
00146 desired_access = FILE_READ_DATA;
00147 } else {
00148 desired_access = FILE_READ_DATA | FILE_WRITE_DATA;
00149 }
00150
00151 fd = cli_nt_create_full(c, fname, 0,
00152 desired_access,
00153 0x0,
00154 FILE_SHARE_READ|FILE_SHARE_WRITE,
00155 create_disposition,
00156 create_options, 0);
00157 if (fd == -1 && handle != -1) {
00158 printf("ERROR: cli_nt_create_full failed for %s - %s\n",
00159 fname, cli_errstr(c));
00160 exit(1);
00161 }
00162 if (fd != -1 && handle == -1) {
00163 printf("ERROR: cli_nt_create_full succeeded for %s\n", fname);
00164 exit(1);
00165 }
00166 if (fd == -1) return;
00167
00168 for (i=0;i<MAX_FILES;i++) {
00169 if (ftable[i].handle == 0) break;
00170 }
00171 if (i == MAX_FILES) {
00172 printf("(%d) file table full for %s\n", line_count,
00173 fname);
00174 exit(1);
00175 }
00176 ftable[i].handle = handle;
00177 ftable[i].fd = fd;
00178 }
00179
00180 void nb_writex(int handle, int offset, int size, int ret_size)
00181 {
00182 int i;
00183
00184 if (buf[0] == 0) memset(buf, 1, sizeof(buf));
00185
00186 i = find_handle(handle);
00187 if (cli_write(c, ftable[i].fd, 0, buf, offset, size) != ret_size) {
00188 printf("(%d) ERROR: write failed on handle %d, fd %d \
00189 errno %d (%s)\n", line_count, handle, ftable[i].fd, errno, strerror(errno));
00190 exit(1);
00191 }
00192
00193 children[nbio_id].bytes_out += ret_size;
00194 }
00195
00196 void nb_readx(int handle, int offset, int size, int ret_size)
00197 {
00198 int i, ret;
00199
00200 i = find_handle(handle);
00201 if ((ret=cli_read(c, ftable[i].fd, buf, offset, size)) != ret_size) {
00202 printf("(%d) ERROR: read failed on handle %d ofs=%d size=%d res=%d fd %d errno %d (%s)\n",
00203 line_count, handle, offset, size, ret, ftable[i].fd, errno, strerror(errno));
00204 exit(1);
00205 }
00206 children[nbio_id].bytes_in += ret_size;
00207 }
00208
00209 void nb_close(int handle)
00210 {
00211 int i;
00212 i = find_handle(handle);
00213 if (!cli_close(c, ftable[i].fd)) {
00214 printf("(%d) close failed on handle %d\n", line_count, handle);
00215 exit(1);
00216 }
00217 ftable[i].handle = 0;
00218 }
00219
00220 void nb_rmdir(const char *fname)
00221 {
00222 if (!cli_rmdir(c, fname)) {
00223 printf("ERROR: rmdir %s failed (%s)\n",
00224 fname, cli_errstr(c));
00225 exit(1);
00226 }
00227 }
00228
00229 void nb_rename(const char *oldname, const char *newname)
00230 {
00231 if (!cli_rename(c, oldname, newname)) {
00232 printf("ERROR: rename %s %s failed (%s)\n",
00233 oldname, newname, cli_errstr(c));
00234 exit(1);
00235 }
00236 }
00237
00238
00239 void nb_qpathinfo(const char *fname)
00240 {
00241 cli_qpathinfo(c, fname, NULL, NULL, NULL, NULL, NULL);
00242 }
00243
00244 void nb_qfileinfo(int fnum)
00245 {
00246 int i;
00247 i = find_handle(fnum);
00248 cli_qfileinfo(c, ftable[i].fd, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
00249 }
00250
00251 void nb_qfsinfo(int level)
00252 {
00253 int bsize, total, avail;
00254
00255 cli_dskattr(c, &bsize, &total, &avail);
00256 }
00257
00258 static void find_fn(const char *mnt, file_info *finfo, const char *name, void *state)
00259 {
00260
00261 }
00262
00263 void nb_findfirst(const char *mask)
00264 {
00265 cli_list(c, mask, 0, find_fn, NULL);
00266 }
00267
00268 void nb_flush(int fnum)
00269 {
00270 int i;
00271 i = find_handle(fnum);
00272
00273 }
00274
00275 static int total_deleted;
00276
00277 static void delete_fn(const char *mnt, file_info *finfo, const char *name, void *state)
00278 {
00279 char *s, *n;
00280 if (finfo->name[0] == '.') return;
00281
00282 n = SMB_STRDUP(name);
00283 n[strlen(n)-1] = 0;
00284 asprintf(&s, "%s%s", n, finfo->name);
00285 if (finfo->mode & aDIR) {
00286 char *s2;
00287 asprintf(&s2, "%s\\*", s);
00288 cli_list(c, s2, aDIR, delete_fn, NULL);
00289 nb_rmdir(s);
00290 } else {
00291 total_deleted++;
00292 nb_unlink(s);
00293 }
00294 free(s);
00295 free(n);
00296 }
00297
00298 void nb_deltree(const char *dname)
00299 {
00300 char *mask;
00301 asprintf(&mask, "%s\\*", dname);
00302
00303 total_deleted = 0;
00304 cli_list(c, mask, aDIR, delete_fn, NULL);
00305 free(mask);
00306 cli_rmdir(c, dname);
00307
00308 if (total_deleted) printf("WARNING: Cleaned up %d files\n", total_deleted);
00309 }
00310
00311
00312 void nb_cleanup(void)
00313 {
00314 cli_rmdir(c, "clients");
00315 children[nbio_id].done = 1;
00316 }