END_TEST START_TEST(test_trace_read_balances) { int status = trace_open("test/4_2_10.trace"); ck_assert_msg(status != -1, "trace_open should not return -1 if the file does exists"); Command cmd; int balances = 0; cmd_t c; int i, f, t, a; while (trace_read_cmd(&cmd) != 0) { cmd_unpack(&cmd, &c, &i, &f, &t, &a); switch (c) { case BALANCE: balances++; break; } } ck_assert_msg(balances == 3, "there should be 3 balance messages, received %d", balances); trace_close(); }
END_TEST START_TEST(test_trace_read_deposits) { int status = trace_open("test/4_2_10.trace"); ck_assert_msg(status != -1, "trace_open should not return -1 if the file does exists"); Command cmd; int deposits = 0; cmd_t c; int i, f, t, a; while (trace_read_cmd(&cmd) != 0) { cmd_unpack(&cmd, &c, &i, &f, &t, &a); switch (c) { case DEPOSIT: deposits++; break; } } ck_assert_msg(deposits == 2, "there should be 2 deposit messages, received %d", deposits); trace_close(); }
END_TEST START_TEST(test_trace_read_transfers) { int status = trace_open("test/4_2_10.trace"); ck_assert_msg(status != -1, "trace_open should not return -1 if the file does exists"); Command cmd; int transfers = 0; cmd_t c; int i, f, t, a; while (trace_read_cmd(&cmd) != 0) { cmd_unpack(&cmd, &c, &i, &f, &t, &a); switch (c) { case TRANSFER: transfers++; break; } } ck_assert_msg(transfers == 4, "there should be 4 transfer message, received %d", transfers); trace_close(); }
END_TEST START_TEST(test_trace_open_good) { int status = trace_open("test/4_2_10.trace"); ck_assert_msg(status != -1, "trace_open should not return -1 if the file does exists"); }
END_TEST START_TEST(test_trace_read_valid_commands) { int status = trace_open("test/4_2_10.trace"); ck_assert_msg(status != -1, "trace_open should not return -1 if the file does exists"); Command cmd; cmd_t c; int i, f, t, a; while (trace_read_cmd(&cmd) != 0) { cmd_unpack(&cmd, &c, &i, &f, &t, &a); switch (c) { case CONNECT: case EXIT: case DEPOSIT: case WITHDRAW: case TRANSFER: case BALANCE: break; default: ck_abort_msg("expected valid command, received %d", c); } } trace_close(); }
END_TEST START_TEST(test_trace_account_count) { int status = trace_open("test/4_2_10.trace"); ck_assert_msg(status != -1, "trace_open should not return -1 if the file does exists"); int account_cnt = trace_account_count(); ck_assert_msg(account_cnt == 2, "account count should be 2, received %d", account_cnt); }
END_TEST ////////////////////////////////////////////////////////////////////// ///////////// trace unit tests ////////////////////////////////////////////////////////////////////// START_TEST(test_trace_open_bad) { int status = trace_open("test/no_trace_file.trace"); ck_assert_msg(status == -1, "trace_open should return -1 if the file does not exist"); }
END_TEST START_TEST(test_trace_read_single_cmd) { int status = trace_open("test/4_2_10.trace"); ck_assert_msg(status != -1, "trace_open should not return -1 if the file does exists"); Command cmd; trace_read_cmd(&cmd); cmd_t c; int i, f, t, a; cmd_unpack(&cmd, &c, &i, &f, &t, &a); ck_assert_msg(c == CONNECT, "first command should be CONNECT, received %d", c); }
int trace_create_index(const char * trace_path) { FILE * idx_stream; static char idx_file[128] = {0}; size_t num_written; off_t curr_filepos = 0; x86_inst_t insn; /* Open the trace */ trace_interface_t * trace = trace_open(trace_path); if (trace == NULL) { return -1; } // Check that the path does not overflow the buffer num_written = snprintf(idx_file, sizeof(idx_file), "%s.idx", trace_path); if (num_written == (sizeof(idx_file) - 1)) { fprintf(stderr, "libtracereader: file path longer than maximum %d char.\n", sizeof(idx_file)); return -1; } // Create index file if ((idx_stream = fopen(idx_file, "w")) == NULL) { fprintf(stderr, "error : could not create index file.\n"); return -1; } // Populate index while (1) { curr_filepos = ftello(trace->trace_stream); if (trace->trace_read_next_insn(trace, &insn, 0) == 0) { fwrite(&(curr_filepos), 8, 1, idx_stream); } else { break; } } // Close trace trace_close(trace); // Close trace index (do not assume trace_open will) fclose(idx_stream); return 0; }
int atm_run(char *trace, int bank_out_fd, int atm_in_fd, int atm_id) { int status = trace_open(trace); if (status == -1) { error_msg(ERR_BAD_TRACE_FILE, "could not open trace file"); return ERR_BAD_TRACE_FILE; } byte cmd[MESSAGE_SIZE]; while (trace_read_cmd(cmd) != 0) { status = atm(bank_out_fd, atm_in_fd, atm_id, cmd); // We continue if the ATM as unknown. This is ok because the trace // file contains commands for all the ATMs. if (status == ERR_UNKNOWN_ATM) { continue; } // We display an error message to the ATM user if the account // is not valid. if (status == ERR_UNKNOWN_ACCOUNT) { printf("ATM error: unknown account! ATM Out of service\n"); continue; } // We display an error message to the ATM user if the account // does not have sufficient funds. if (status == ERR_NOFUNDS) { printf("not enough funds, retry transaction\n"); continue; } // If we receive a status that is not successful we return with // the status. if (status != SUCCESS) { printf("status is %d\n", status); return status; } } trace_close(); return SUCCESS; }
int main(int argc, char *argv[]) { if (argc != 2) { printf("usage: %s trace_file\n", argv[0]); exit(1); } int result = trace_open(argv[1]); if (result == -1) { printf("%s: could not open %s\n", argv[0], argv[1]); exit(1); } printf("number of ATMs: %d\n", trace_atm_count()); printf("number of accounts: %d\n", trace_account_count()); byte cmd[MESSAGE_SIZE]; while (trace_read_cmd(cmd) != 0) { cmd_dump("TREADER", 0, cmd); } trace_close(); return 0; }
HRESULT GRF_File::Load(LPCWSTR fileName) { HRESULT hr = S_OK; STATSTG rGrfStat; CComPtr<IStorage> pGrfFile; CComPtr<IStream> pGrfData; Clear(); TRACE(trace_open(NULL,zFalse)); TRACE(trace(TEXT("*** StgOpenStorage(L\"%") TRACEWSTR TEXT("\")...\n"), fileName)); if(S_OK != (hr = StgOpenStorage(fileName,NULL,STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE,NULL,DWRESERVED,&pGrfFile))) return hr; TRACE(trace(TEXT("*** OpenStream(L\"ActiveMovieGraph\")...\n"))); if(S_OK != (hr = pGrfFile->OpenStream(L"ActiveMovieGraph",LPRESERVED,STGM_DIRECT|STGM_READ|STGM_SHARE_EXCLUSIVE,DWRESERVED,&pGrfData))) return hr; TRACE(trace(TEXT("*** Stat()...\n"))); if(S_OK != (hr = pGrfData->Stat(&rGrfStat,STATFLAG_NONAME))) return hr; int cb = (int)rGrfStat.cbSize.QuadPart; TRACE(trace(TEXT("*** new BYTE[%d]...\n"),cb)); const int EXTRA = 32 * sizeof(WCHAR); // add safety margin to end of buffer CStringA data('\0', cb + EXTRA); char* const pb = data.GetBufferSetLength(cb + EXTRA); if (!pb) return E_POINTER; TRACE(trace(TEXT("*** Read(%d)...\n"),cb)); if(S_OK != (hr = pGrfData->Read(pb,(ULONG)cb,NULL))) return hr; LPCWSTR p = (LPCWSTR)pb; LPCWSTR s1 = p, s2 = p; const LPCWSTR end = p + 1 + (cb/sizeof(WCHAR)); // round up to next WCHAR after cb bytes p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(">>> version = %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); const long format_version = wcstol(s1, NULL, 10); ASSERT(format_version == 3); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; if(0 == wcsnicmp(s1,L"FILTERS",s2-s1)) { int filter_index = 1; TRACE(trace(TEXT(">>> FILTERS\n"))); while (p < end) { p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; if(0 == wcsnicmp(s1,L"CONNECTIONS",s2-s1)) break; GRF_Filter grf_filter; TRACE(trace(TEXT(">>> %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); grf_filter.index = wcstol(s1, NULL, 10); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(">>> name .... = L\"%.*") TRACEWSTR TEXT("\"\n"), (INT) (s2 - s1), s1)); grf_filter.name = CString(s1, (int) (s2-s1)); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(">>> class_id = %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); GUID clsid = {0}; const CString clsid_str(s1, (int) (s2-s1)); hr = CLSIDFromString(clsid_str, &grf_filter.clsid); // TODO is it possible for filter to be both source and sink? p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; const bool source_file = 0 == wcsnicmp(s1,L"SOURCE",s2-s1); const bool sink_file = 0 == wcsnicmp(s1,L"SINK",s2-s1); if (source_file || sink_file) { TRACE(trace(TEXT(">>> file .... = %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(">>> file_name = L\"%.*") TRACEWSTR TEXT("\"\n"), (INT) (s2 - s1), s1)); CStringW filename = CString(s1, (int) (s2-s1)); if (source_file) { grf_filter.source_filename = filename; } else { grf_filter.sink_filename = filename; } p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; } TRACE(trace(TEXT(">>> length .. = %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); int filter_data_length = 0; const char* filter_data = (const char*)(p + 1); if(S_OK != (hr = skip(s1, &p, filter_data_length))) return hr; if (p >= end) return E_NOT_SUFFICIENT_BUFFER; grf_filter.ipersiststream_data = CStringA(filter_data, filter_data_length); ASSERT(filter_data_length == grf_filter.ipersiststream_data.GetLength()); grf_filters.Add(grf_filter); } } if(0 == wcsnicmp(s1,L"CONNECTIONS",s2-s1)) { TRACE(trace(TEXT(">>> CONNECTIONS\n"))); while(p < end) { p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; if(0 == wcsnicmp(s1,L"CLOCK",s2-s1)) break; if(0 == wcsnicmp(s1,L"END",s2-s1)) break; GRF_Connection grf_connection; TRACE(trace(TEXT(">>> %.*") TRACEWSTR, (INT) (s2 - s1), s1)); grf_connection.output_filter_index = wcstol(s1, NULL, 10); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(" [L\"%.*") TRACEWSTR TEXT("\"]"), (INT) (s2 - s1), s1)); grf_connection.output_pin_id = CString(s1, (int) (s2-s1)); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(" -> %.*") TRACEWSTR, (INT) (s2 - s1), s1)); grf_connection.input_filter_index = wcstol(s1, NULL, 10); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(" [L\"%.*") TRACEWSTR TEXT("\"]\n"), (INT) (s2 - s1), s1)); grf_connection.input_pin_id = CString(s1, (int) (s2-s1)); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(">>> sample_size ........ = %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); grf_connection.flags = wcstol(s1, NULL, 10); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(">>> major_type ......... = %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); CString guid_str(s1, (int) (s2-s1)); hr = CLSIDFromString(guid_str, &grf_connection.media_type.majortype); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(">>> subtype ............ = %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); guid_str = CString(s1, (int) (s2-s1)); hr = CLSIDFromString(guid_str, &grf_connection.media_type.subtype); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(">>> fixed_sample_size .. = %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); grf_connection.media_type.bFixedSizeSamples = wcstol(s1, NULL, 10); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(">>> temporal_compression = %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); grf_connection.media_type.bTemporalCompression = wcstol(s1, NULL, 10); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(">>> length(?) .......... = %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); grf_connection.media_type.lSampleSize = wcstol(s1, NULL, 10); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(">>> format_type ........ = %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); guid_str = CString(s1, (int) (s2-s1)); hr = CLSIDFromString(guid_str, &grf_connection.media_type.formattype); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(">>> length ............. = %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); int format_length = 0; BYTE* format_data = (BYTE *)(p + 1); if(S_OK != (hr = skip(s1, &p, format_length))) return hr; if (p >= end) return E_NOT_SUFFICIENT_BUFFER; if (format_length > 0) grf_connection.media_type.SetFormat(format_data, format_length); grf_connections.Add(grf_connection); p++; /// where is this L'\0' terminator documented?? } } if(0 == wcsnicmp(s1,L"CLOCK",s2-s1)) { TRACE(trace(TEXT(">>> CLOCK\n"))); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(">>> required = %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); clock_flags = wcstol(s1, NULL, 10); p = token(p,end,&s1,&s2); if (p >= end) return E_NOT_SUFFICIENT_BUFFER; TRACE(trace(TEXT(">>> clock_id = %.*") TRACEWSTR TEXT("\n"), (INT) (s2 - s1), s1)); clock_index = wcstol(s1, NULL, 10); } if(0 == wcsnicmp(s1,L"END",s2-s1)) { TRACE(trace(TEXT(">>> END\n"))); } TRACE(trace(TEXT("*** hr = ") TRACEHR_STR TEXT("\n"),TRACEHR_REF(hr))); TRACE(trace_close()); return hr; }
int main(int argc, char ** argv) { int opt = -1; int long_index =0; char *endptr = NULL; size_t len; static struct option long_options[] = { {"count", no_argument, 0, 'c'}, {"createindex", no_argument, 0, 'd'}, {"first", required_argument, 0, 'f'}, {"header", no_argument, 0, 'h'}, {"intel", no_argument, 0, 'i'}, {"last", required_argument, 0, 'l'}, {"out", required_argument, 0, 'o'}, {"trace", required_argument, 0, 't'}, {"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0} }; while ((opt = getopt_long_only(argc, argv, "", long_options, &long_index )) != -1) { switch (opt) { case 'c': print_counter = 1; break; case 'd': create_index = 1; break; case 'f': first = strtoull(optarg, &endptr, 0); len = strlen(optarg); if (endptr != optarg + len) { fprintf(stderr, "error : invalid -first <int64> argument.\n"); return -1; } break; case 'h': print_header = 1; break; case 'i': intel_format = 1; break; case 'l': last = strtoull(optarg, &endptr, 0); len = strlen(optarg); if (endptr != optarg + len) { fprintf(stderr, "error : invalid -last <int64> argument.\n"); return -1; } break; case 'o': sprintf(output_file, "%s", optarg); break; case 't': sprintf(trace_file, "%s", optarg); break; case 'v': verbose = 1; break; default: print_usage(); return -1; } } if(trace_file[0] == 0) { print_usage(); return -1; } if(output_file[0] != 0 && (out_stream = fopen(output_file, "w")) == NULL) { fprintf(stderr, "error : output file not accessible.\n"); return -1; } if(out_stream == NULL) { out_stream = stdout; } setbuf(out_stream, outbuf); /* If create index requested, loop over instructions and exit */ if (create_index) { return trace_create_index(trace_file); } /* Open trace */ trace_interface_t * trace_iface = trace_open(trace_file); if (trace_iface == NULL) { return -1; } /* If requested, print header and exit */ if (print_header) { print_trace_header(stdout, trace_iface, verbose); goto cleanup; } /* Loop over instructions */ trace_fold(trace_iface, first, last, 0, process_insn, trace_iface); /* Update progress */ print_percent(stderr, 100); cleanup: /* Close files */ trace_close(trace_iface); fclose(out_stream); return 0; }
int main (int argc, char *argv[]) { if (argc != 2) { printf("usage: %s trace_file\n", argv[0]); exit(1); } int result = 0; int atm_count = 0; int account_count = 0; // open the trace file result = trace_open(argv[1]); if (result == -1) { printf("%s: could not open file %s\n", argv[0], argv[1]); exit (1); } // Get the number of ATMs and accounts: atm_count = trace_atm_count(); account_count = trace_account_count(); trace_close(); // Setup bank pipes: int bankfd[2]; pipe(bankfd); // This is a table of atm_out file descriptors. It will be used by // the bank process to communicate to each of the ATM processes. int atm_out_fd[atm_count]; // TODO: ATM PROCESS FORKING // START YOUR IMPLEMENTATION for(int i =0; i < atm_count; i++){ int atmfd[2]; pipe(atmfd); atm_out_fd[i] =atmfd[1]; if(fork()== 0){ close(atmfd[1]); close(bankfd[0]); int result = atm_run(argv[1],bankfd[1],atmfd[0],i); if(result != SUCCESS){ error_print(); } else{ close(atmfd[0]); } exit(0); } } // END YOUR IMPLEMENTATION // TODO: BANK PROCESS FORKING // START YOUR IMPLEMENTATION //pid_t pid2 = fork(); if(fork() == 0){ close(bankfd[1]); bank_open(atm_count,account_count); int result =run_bank(bankfd[0],atm_out_fd); if(result != SUCCESS){ error_print(); } bank_dump(); bank_close(); exit(0); } // END YOUR IMPLEMENTATION // Wait for each of the child processes to complete. We include // atm_count to include the bank process (i.e., this is not a // fence post error!) for (int i = 0; i <= atm_count; i++) { wait(NULL); } return 0; }