CIS 212 Project 5Instrumenting a program to produce a profileDue at 11:59pm on Monday, 6 May 2019This project requires that you instrument three source files of a program that I provide to you inorder to determine the number of times each function is called and the total amount of elapsedtime spent in each function.Section 6.1.1 of the textbook describes how to use /usr/bin/time to obtain performancecharacteristics for a program’s execution from the shell. Section 6.1.2 discusses how tomeasure elapsed time of various portions of a program by inserting source code into theprogram. You will be inserting such instrumentation into a working version of thewordperline program from Project 4.1 RequirementsThe source files that you need to modify are jsheap.c, jsstring.c, andwordperline.c. Your inserted instrumentation will keep track of the number of calls andthe elapsed time spent in each of the functions in jsheap.c and jsstring.c. Yourinserted code in wordperline.c will perform any required initialization of global datastructures after processing arguments, and after all processing has completed, will print out theperformance summary for all of the instrumented functions on stderr.The files stringADT.c, arraylist.c, and iterator.c have been edited to callroutines in jsheap.c and jsstring.c. This way, as wordperline uses the functions inthese files while doing its work, your instrumentation can observe those calls. As with Project 4,you may not modify the ADT .h or .c files provided to you; you will have to compile them andlink them together with your modified jsheap.o, jsstring.o, and wordperline.o toproduce the instrumented version of wordperline. The Makefile provided does theappropriate assembly of the program.2 Starting filesIn Canvas, in Files/Projects, you will find a gzipped tar archive named project5start.tgz;this file contains the following files: input – a file that contains text; this is the same file used in Project 1. input1, input2 – other files containing text; these are the same files used inProjects 2 and 3. tscript – a bash script file that performs a number of tests of your wordperlineusing input, input1, input2, and test*.out; invoking ./tscriptwill execute all of the tests using your executable wordperline. test*.out – the correct output files for the tests in tscript.CIS 212 Project 5 arraylist.[ch] – header and source file for the ArrayList ADT stringADT.[ch] – header and source file for the String ADT iterator.[ch] – header and source file for the Iterator ADT, needed byArrayList jsheap.[ch] – wrapper functions that invoke malloc() and free() jsstring.[ch] – wrapper functions that invoke strchr(), strcmp(),strcpy(), strdup(), strlen(), strncmp(), and strstr() itest.c – a simple test program showing how to instrument source code Makefile3 HintsI suggest that you thoroughly review Section 6.1 of the textbook on measuring running time. Ihave included additional information below showing how such instrumentation can be done ona single function in a single source file.4 Checking that your solution works correctlytscript takes zero or more arguments; if one or more arguments are specified, tscriptwill only perform those specific tests; if no arguments are specified, all of the tests areperformed.The tests that tscript performs are:a Tests that the call and elapsed time data for processing input are correct.b Tests that the call and elapsed time data for processing input1 are correct.c Tests that the call and elapsed time data for processing input2 are correct.d Tests that the call and elapsed time data for processing input1 and input2 arecorrect.e Tests that the call and elapsed time data for processing 5 copies of input arecorrect.f Tests that the call and elapsed time data for processing 10 copies of input arecorrect.g Tests that the call and elapsed time data for processing 20 copies ofinput arecorrect.For each test, tscript prints out “Starting test ”, where isone of {a, b, c, d, e, f, g }; it then prints a 1-line description of what it is testing; itthen prints the actual command line that it is executing; finally, it prints“Stopping test ”.If there is any output between the actual command line and the Stopping line for any test, thetest has failed.CIS 212 Project 5-3-5 Submission1You will submit your solutions electronically by uploading a gzipped tar archive2 via Canvas.Your TGZ archive should be named -project5.tgz, where is your“duckid”. It should contain your files wordperline.c, jsheap.c, and jsstring.c; if youcreated any .h files that these three source files used to share information, it needs to beincluded; finally, it should also contain a file named report.txt; this file should contain yourname, duckid, the names of any classmates who helped you and what assistance they provided,and the current state of your solution. Do not include arraylist.[ch],stringADT.[ch], iterator.[ch], and Makefile in your archive; I will use thoseprovided to you.6 itest.c as an instrumentation exampleThis section provides annotated source code of a test program, provided in the start archive,showing you how such instrumentation can be performed.6.1 preliminaries/** itest - show how to instrument a function and print the results** usage: ./itest [--calls=]** the instrumented function will be called `calls times; default is 1000** outputs number of calls and ms of elapsed time on stderr** Author: Joe Sventek*/#include /* for fprintf() */#include /* for strncmp() */#include /* for atoi() */#include /* for struct timespec & nanosleep() */#include /* for struct timeval & gettimeofday() */#define USESTR usage: %s [--calls=n]\nThe comments describe what the program does and how to invoke the program.The #include lines are to bring in needed structure definitions and function signatures,noted in the comments to the right of each #include.Finally, we #define a format string to use if the user has supplied illegal arguments.1 If you do not follow these submission instruction, I will NOT mark your submission and you will receive a 0 forthe project.2 See section 7 of Canvas/Files/Projects/P1Handout.pdf for instructions if you do not remember how to create agzipped tar archive. Obviously, the filenames used for this project will be different.CIS 212 Project 5-4-6.2 Needed structure declaration[s] and global variablestypedef struct accumulator { char *name; long long calls; long long musecs;} Accumulator;long nAccumulators = 0L;Accumulator *accumulators[1000];We define a structure (Accumulator) where we keep track of: the name of a function the number of calls on the function the elapsed time in the function in microsecondsThere will be one of these structures for each function we are instrumenting.We also declare two global variables in which we keep track of the accumulators: nAccumulators – this is the number of accumulators that have been “registered”with the system CIS 212作业代写、profile留学生作业代做、c/c++实验作业代写、代做c/c++程序设计作业 调试Matlaaccumulators – this is an array of pointers to the registered accumulatorsWe have sized this array to hold 1000 entries; this is more than enough to cope with thisproject.6.3 The instrumented functionvoid instrumented_function(void) { struct timeval t1, t2; static int init = 1; static Accumulator ac = {instrumented_function, 0LL, 0LL}; if (init) { accumulators[nAccumulators++] = ∾ init = 0; } (void)gettimeofday(&t1, NULL); { struct timespec oneMs = {0, 1000000L}; /* one millisecond */ nanosleep(&oneMs, NULL); } (void)gettimeofday(&t2, NULL); ac.musecs += 1000000 * (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec); ac.calls += 1LL;}The actual body of the function is the block that declares oneMs and calls nanosleep();everything else is needed to register the accumulator, measure the elapsed time, and updatethe calls and musecs fields of the accumulator.CIS 212 Project 5-5-6.3.1 Registering the accumulator static int init = 1; static Accumulator ac = {instrumented_function, 0LL, 0LL}; if (init) { accumulators[nAccumulators++] = ∾ init = 0; }The first two lines show you another use of the static keyword in C. Recall that when we useit on global variables and on functions in a particular source file, it hides the existence of thosenames from the linker, so that code in other source files that may be linked to this source filecannot access those entities.This use of static causes variables declared inside of a block (in this case a function block) tobe allocated in global memory but the names are hidden within the block – in this case, onlyvisible in the function. Since these variables are in global memory, and not on the stack, theyretain their value between calls of the function.The first time the function is called, init has a value of 1, corresponding to True in C. We thenexecute the statements in the first if statement. Since the last of those statements sets initto 0 (False), and since init retains its value between calls to the function, subsequent calls tothe function will not execute the statements in the first if statement.The only other statement executed in that if clause is to register the accumulator – we simplycopy the address of our accumulator into the global accumulators[] array, incrementingthe nAccumulators variable.6.3.2 Determining the start and stop times (void)gettimeofday(&t1, NULL); { struct timespec oneMs = {0, 1000000L}; /* one millisecond */ nanosleep(&oneMs, NULL); } (void)gettimeofday(&t2, NULL);gettimeofday() returns the current time as seconds and microseconds since a particulartime in the past. t1 contains the current time before executing the actual statements of ourfunction, and t2 contains the current time after executing those statements.6.3.3 Update the accumulator ac.musecs += 1000000 * (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec); ac.calls += 1LL;}A struct timeval has two members: tv_sec which contains an integer representingseconds, and tv_usec, an integer representing number of microseconds. We compute thenumber of microseconds that have elapsed between t1 and t2, and add this to the musecsmember of our accumulator. We also increment the calls member of our accumulator.CIS 212 Project 5-6-6.4 Dump the accumulatorsvoid dumpAccumulators(FILE *fd) { long k; for (k = 0L; k Accumulator *ac = accumulators[k]; long long musecs = ac->musecs; fprintf(fd, %-20s , ac->name); fprintf(fd, %12Ld calls , ac->calls); fprintf(fd, %15Ld.%03dms\n, musecs / 1000, (int)(musecs % 1000)); }}This function simply goes through the list of accumulators, printing out the data found in eachone. The format strings indicate that: the name of the function should be printed out left justified in a 20-character wide field; the number of calls should be printed out as a decimal integer right justified in a 12-character wide field; the elapsed time should be printed out as the number of milliseconds right justified in a15-character wide field, followed by a decimal point, followed by the number ofmicroseconds left over in a 3-character wide field, using a ‘0’ to right justify that numberin the field; the resulting printout looks like a floating point number with 3 decimalplaces.6.5 The main() functionint main(int argc, char *argv[]) { int i, n = 1000; for (i = 1; i if (strncmp(argv[i], --calls=, 8) == 0) n = atoi(argv[i]+8); else { fprintf(stderr, Illegal flag: %s\n, argv[i]); fprintf(stderr, USESTR, argv[0]); return 1; } } /* initialize the instrumentation system – nothing to do */ for (i = 0; i instrumented_function(); } dumpAccumulators(stderr); return 0;}First we process command arguments. Note the use of strncmp() to process long flags.Next we initialize the instrumentation system, if needed. Given the way we have constructedthe global variables and the Accumulator registration in the instrumented function[s], there isnothing to do here.CIS 212 Project 5-7-Next we perform the functionality of our program – in this case, it is simply to invoke theinstrumented function the specified number of times.Finally, we call dumpAccumulators() to display the collected information on stderr.CIS 212 Project 5-1-Grading RubricYour submission will be marked on a 50 point scale. Substantial emphasis is placed uponWORKING submissions, and you will note that a large fraction of the points are reserved for thisaspect. It is to your advantage to ensure that whatever you submit compiles, links, and runscorrectly. The information returned to you will indicate the number of points awarded for thesubmission.You must be sure that your code works correctly on the virtual machine under VirtualBox,regardless of which platform you use for development and testing. Leave enough time in yourdevelopment to fully test on the virtual machine before submission.The marking scheme is as follows:Points Description5 Your report – honestly describes the state of your submissionYour submission passes test a.Your submission passes test b.Your submission passes test c.Your submission passes test d.Your submission passes test e.Your submission passes test f.Your submission passes test g.modified wordperline.c, jsheap.c, and jsstring.c successfully compilemodified wordperline.c, jsheap.c, and jsstring.c compile with no warningsThe link phase to create wordperline is successfulThe link phase to create wordperline is successful with no warningsThere are no memory leaks in your programThe code could have worked with minor modification to the source.Note that: Your report needs to be honest. Stating that everything works and then finding that itdoesn’t is offensive. The 5 points associated with the report are probably the easiest 5points you will ever earn as long as you are honest. The 10 points for “could have worked” is the maximum awarded in this category; yourmark in this category may be lower depending upon how close you were to a workingimplementation.转自:http://www.7daixie.com/2019050322005171.html