def readMeasurementsFromFile(location): """Reads, from a file, the measurements of feasible paths. Arguments: location: Location of the file that contains measurements of feasible paths. Returns: List of the measurements of feasible paths. """ try: measurementsFileHandler = open(location, "r") except EnvironmentError as e: errMsg = ("Error reading the measurements from the file located " "at %s: %s" % (location, e)) raise GameTimeError(errMsg) else: with measurementsFileHandler: measurements = [] measurementLines = measurementsFileHandler.readlines() for measurementLine in measurementLines: _, measurement = measurementLine.strip().split() measurements.append(float(measurement)) return measurements
def _convertToSrec(self, outFileLocation): """Converts the binary file produced by the compilation of a temporary file that contains a test case to the SREC format executed by the simulator. Arguments: outFileLocation: Location of the binary file produced by the compilation of a temporary file that contains a test case. """ srecFileLocation = os.path.join(self._measurementDir, "thread0.srec") srecCmd = [] srecCmd.append("%s/bin/arm-elf-objcopy" % config.SIMULATOR_TOOL_GNU_ARM) srecCmd.append("--output-target") srecCmd.append("srec") srecCmd.append(outFileLocation) srecCmd.append(srecFileLocation) returnCode = subprocess.call(srecCmd) if returnCode: errMsg = ("Error in converting the binary file produced by " "compilation to the SREC format executed by " "the simulator (arm-elf-objcopy).") raise GameTimeError(errMsg)
def writeMeasurementsToFile(self, location, paths): """Measures the values of the paths in the list provided, and records these values in the file whose location is provided. The format of the file is the same as that expected by the ``loadBasisValuesFromFile`` method of the ``Analyzer`` class. Arguments: location: Location of the file where the values will be recorded. paths: List of paths whose values need to be measured, each represented by a :class:`~gametime.path.Path` object. """ logger.info("Measuring the values of paths on the%ssimulator..." % (" " if self.name == "" else (" %s " % self.name))) try: measurementsFileHandler = open(location, "w") except EnvironmentError as e: errMsg = ("Error writing the path values to the file located " "at %s: %s" % (location, e)) raise GameTimeError(errMsg) else: with measurementsFileHandler: measurements = self.measurePaths(paths) for pathNum, value in enumerate(measurements): measurementsFileHandler.write("%d\t%d\n" % ((pathNum + 1), value)) logger.info("Measurement of all path values complete.")
def measure(self, path): """ Arguments: path: :class:`~gametime.path.Path` object that represents the path whose cycle count needs to be measured. Returns: Cycle count of the path, as measured on the PTARM simulator. """ # Create the temporary directory where the temporary files generated # during measurement will be stored. createDir(self._measurementDir) try: # This sequence of function calls closely mimics the steps # described at # http://chess.eecs.berkeley.edu/pret/src/ptarm-1.0/\ # ptarm_simulator.html#%5B%5BCompiling%20Programs%5D%5D. testCaseFileLocation = self._createTestCaseFile(path) outFileLocation = self._compileFile(testCaseFileLocation) asmFileLocation = self._dumpAsmFile(outFileLocation) self._convertToSrec(outFileLocation) self._copyEvecSrec() cycleCount = self._runSimulatorAndParseOutput(asmFileLocation) except EnvironmentError as e: errMsg = ("Error in measuring the cycle count of a path " "when simulated on the PTARM simulator: %s" % e) raise GameTimeError(errMsg) if not self.projectConfig.debugConfig.KEEP_SIMULATOR_OUTPUT: self._removeTemps() return cycleCount
def _compileFile(self, testCaseFileLocation): """Compiles the temporary file that contains a test case using the cross-compiler for the ARM target (arm-elf-gcc). Arguments: testCaseFileLocation: Location of the temporary file that contains the test case. Returns: Location of the binary output file produced by the compilation. """ projectConfig = self.projectConfig outFileLocationNoExt, _ = os.path.splitext(testCaseFileLocation) outFileLocation = "%s.out" % outFileLocationNoExt compileCmd = [] # This command prefix was suggested by # http://sharats.me/the-ever-useful-and-neat-subprocess-module.html. compileCmd.append("mintty") compileCmd.append("--hold") compileCmd.append("error") compileCmd.append("--exec") compileCmd.append("%s/bin/arm-elf-gcc" % config.SIMULATOR_TOOL_GNU_ARM) compileCmd.append("-I./") for includedFileLocation in projectConfig.included: compileCmd.append("-I'%s'" % includedFileLocation) compileCmd.append("-I'%s/include'" % config.SIMULATOR_TOOL_GNU_ARM) compileCmd.append("-I'%s/include'" % config.SIMULATOR_PTARM) compileCmd.append("-I'%s/tests/include'" % config.SIMULATOR_PTARM) compileCmd.append("-nostartfiles") compileCmd.append("-g") compileCmd.append("-mcpu=arm7di") compileCmd.append("-DSTACK_INIT=0x40100000") compileCmd.append("'%s/tests/crt/crt0.S'" % config.SIMULATOR_PTARM) compileCmd.append("-Ttext") compileCmd.append("0x40000000") compileCmd.append("-L'%s/lib'" % config.SIMULATOR_TOOL_GNU_ARM) compileCmd.append(testCaseFileLocation) compileCmd.append("-o") compileCmd.append(outFileLocation) returnCode = subprocess.call(compileCmd) if returnCode: errMsg = ("Error in compiling using the cross-compiler for " "the ARM target (arm-elf-gcc).") raise GameTimeError(errMsg) return outFileLocation
def measure(self, path): """ Arguments: path: :class:`~gametime.path.Path` object that represents the path whose cycle count needs to be measured. Returns: Cycle count of the path, as measured on the SimIt-ARM simulator. """ # Create the temporary directory where the temporary files generated # during measurement will be stored. createDir(self._measurementDir) try: if run("mkdir -p %s" % self._remoteMeasurementDir): errMsg = ("Error in creating the temporary directory " "for measurements on the remote computer.") raise GameTimeError(errMsg) testCaseFileLocation = self._createTestCaseFile(path) self._transferFile(testCaseFileLocation) zeroTestCaseFileLocation = self._createZeroTestCaseFile(path) self._transferFile(zeroTestCaseFileLocation) if run("rm -rf %s" % self._remoteMeasurementDir): errMsg = ("Error in removing the temporary directory " "for measurements on the remote computer.") raise GameTimeError(errMsg) except EnvironmentError as e: errMsg = ("Error in measuring the cycle count of a path " "when simulated on the SimIt-ARM simulator: %s" % e) raise GameTimeError(errMsg) # TODO (jokotker): Not completely integrated. return 0
def writeSmtQueryToFile(self, location): """Writes the SMT query associated with this :class:`~gametime.smt.query.Query` object to a file. Arguments: location: Location of the file. """ try: smtQueryFileHandler = open(location, "w") except EnvironmentError as e: errMsg = ("Error writing the SMT query to a file " "located at %s: %s" % (location, e)) raise GameTimeError(errMsg) else: with smtQueryFileHandler: smtQueryFileHandler.write(self.getSmtQuery())
def _transferFile(self, location): """ Transfers the file at the location provided into the temporary directory on the remote machine that stores the temporary files generated during simulation and measurement. Arguments: location: Location of a file on the local machine. """ result = put(location, self._getRemotePath(location), mirror_local_mode=True) if len(result.failed) > 0: errMsg = ("Error in uploading the file located at %s to " "a remote machine: %s" % (location, e)) raise GameTimeError(errMsg)
def writeModelToFile(self, location): """Writes the model generated by an SMT solver in response to the SMT query, if the query is satisfiable, to a file. Arguments: location: Location of the file. """ try: modelFileHandler = open(location, "w") except EnvironmentError as e: errMsg = ("Error writing the model generated by an SMT solver " "in response to the SMT query to a file located at %s: " "%s" % (location, e)) raise GameTimeError(errMsg) else: with modelFileHandler: modelFileHandler.write(self.getModel())
def _getFieldName(self, fieldArrayName): """ Arguments: fieldArrayName: Name of the array for the aggregate field. Returns: Name of an aggregate field from the name of the array for the field. """ matching = re.match( config.IDENT_FIELD + r"(?P<fieldName>(\w+))" + config.IDENT_AGGREGATE, fieldArrayName) if matching is None: errMsg = ("Input name does not have an aggregate field name: %s" % fieldArrayName) raise GameTimeError(errMsg) return matching.group("fieldName")
def writeUnsatCoreToFile(self, location): """Writes the unsatisfiable core generated by an SMT solver in response to the SMT query, if the query is unsatisfiable, to a file. Arguments: location: Location of the file. """ try: unsatCoreFileHandler = open(location, "w") except EnvironmentError as e: errMsg = ("Error writing the unsatisfiable core generated by " "an SMT solver in response to the SMT query to " "a file located at %s: %s" % (location, e)) raise GameTimeError(errMsg) else: with unsatCoreFileHandler: unsatCoreFileHandler.write(self.getUnsatCore())
def getName(satSolver): """ Returns the name of the SAT solver whose SatSolver representation is provided. @param satSolver SatSolver representation of a SAT solver. @retval Name of the SAT solver whose SatSolver representation is provided. """ if satSolver == SatSolver.LINGELING: return "lingeling" elif satSolver == SatSolver.MINISAT: return "minisat" elif satSolver == SatSolver.PICOSAT: return "picosat" else: errMsg = ("Unknown backend SAT solver for Boolector: %s" % satSolver) raise GameTimeError(errMsg)
def readQueryFromFile(location): """Reads an SMT query from the file provided. Arguments: location: Location of the file that contains an SMT query. Returns: :class:`~gametime.smt.query.Query` object that represents the SMT query. """ try: queryFileHandler = open(location, "r") except EnvironmentError as e: errMsg = ("Error reading the SMT query from the file " "located at %s: %s" % (location, e)) raise GameTimeError(errMsg) else: with queryFileHandler: return Query(queryFileHandler.read())
def getSatSolver(satSolverName): """ Returns the SatSolver representation of the SAT solver whose name is provided. @param satSolverName Name of a SAT solver. @retval SatSolver representation of the SAT solver whose name is provided. """ satSolverName = satSolverName.lower() if satSolverName == "lingeling" or satSolverName == "": return SatSolver.LINGELING elif satSolverName == "minisat": return SatSolver.MINISAT elif satSolverName == "picosat": return SatSolver.PICOSAT else: errMsg = ("Unknown backend SAT solver for Boolector: %s" % satSolverName) raise GameTimeError(errMsg)
def checkSat(self, query): """ Checks and updates the satisfiability of the SMT query represented by the Query object provided. If the SMT query is satisfiable, the Query object is updated with a satisfying model; if the query is unsatisfiable, the Query object is updated with an unsatisfiable core. @param query Query object that represents an SMT query. """ solver = z3.Solver() queryExpr = z3.parse_smt2_string(query.queryStr) if (not queryExpr.decl().kind() == z3.Z3_OP_AND or not queryExpr.children()[-1].decl().kind() == z3.Z3_OP_AND): errMsg = "SMT query is not in the form expected." raise GameTimeError(errMsg) # Assert all of the equivalences in the query. # (Ignore the last child of the `And' Boolean expression, # which is not an equivalence.) equivalences = queryExpr.children()[:-1] for equivalence in equivalences: solver.add(equivalence) # Obtain the Boolean variables associated with the constraints. constraintVars = [equivalence.children()[0] for equivalence in equivalences] # Check the satisfiability of the query. querySatResult = solver.check(*constraintVars) if querySatResult == z3.sat: query.labelSat(Model(solver.model().sexpr())) elif querySatResult == z3.unsat: unsatCore = solver.unsat_core() unsatCore = [str(constraintVar) for constraintVar in unsatCore] unsatCore = [int(constraintNumStr[len(config.IDENT_CONSTRAINT):]) for constraintNumStr in unsatCore] query.labelUnsat(unsatCore) else: query.labelUnknown()
def p_error(self, p): errMsg = "Syntax error in input: '%s' " % p.value raise GameTimeError(errMsg)
def t_error(self, t): errMsg = "Illegal character `%s'" % t.value[0] raise GameTimeError(errMsg)