예제 #1
0
    def saveJobDb(self, jobResourceDic):
        print('>>> Saving job related cpu/memory information into sqlite3 ...')
        common.debug('Saving job resource info into sqlite3 ...')

        jobList = list(jobResourceDic.keys())
        jobRangeDic = common.getJobRangeDic(jobList)

        keyList = ['SAMPLE_TIME', 'HOST_NAME', 'CPU', 'MEMORY']
        keyString = sqlite3_common.genSqlTableKeyString(keyList)

        for jobRange in jobRangeDic.keys():
            jobResourceSqlDic = {}
            dbFile = str(self.dbPath) +'/job/' + str(jobRange) + '.db'
            (result, dbConn) = sqlite3_common.connectDbFile(dbFile, mode='write')

            if result == 'passed':
                jobTableList = sqlite3_common.getSqlTableList(dbFile, dbConn)
            else:
                jobTableList = []
                print('*Error*: Failed on connecting sqlite3 database "' + str(dbFile) + '".')
                continue

            for job in jobRangeDic[jobRange]:
                jobResourceSqlDic[job] = {
                                  'drop': False,
                                  'keyString': '',
                                  'valueString': '',
                                 }
                tableName = job
                print('    Sampling for job "' + str(job) + '" ...')

                if tableName in jobTableList:
                    returnCode = self.checkOldSqlTable(dbFile, dbConn, tableName)
                    if returnCode == 1:
                        jobResourceSqlDic[job]['drop'] = True
                        jobResourceSqlDic[job]['keyString'] = keyString
                else:
                    jobResourceSqlDic[job]['keyString'] = keyString

                valueList = [sampleTime, hostname, jobResourceDic[job]['cpu'], jobResourceDic[job]['memory']]
                valueString = sqlite3_common.genSqlTableValueString(valueList)
                jobResourceSqlDic[job]['valueString'] = valueString

            for job in jobResourceSqlDic.keys():
                tableName = job
                if jobResourceSqlDic[job]['drop']:
                    print('    Dropping table "' + str(tableName) + '" ...')
                    sqlite3_common.dropSqlTable(dbFile, dbConn, tableName, commit=False)
                if jobResourceSqlDic[job]['keyString'] != '':
                    print('    Creating table "' + str(tableName) + '" ...')
                    sqlite3_common.createSqlTable(dbFile, dbConn, tableName, jobResourceSqlDic[job]['keyString'], commit=False)
                if jobResourceSqlDic[job]['valueString'] != '':
                    print('    Updating table "' + str(tableName) + '" with content "' + str(jobResourceSqlDic[job]['valueString']) + '" ...')
                    sqlite3_common.insertIntoSqlTable(dbFile, dbConn, tableName, jobResourceSqlDic[job]['valueString'], commit=False)

            if result == 'passed':
                dbConn.commit()
                dbConn.close()
                common.debug('Saving job resource info done.')
예제 #2
0
    def sampleJobInfo(self):
        """
        Sample job info, especially the memory usage info.
        """
        self.getDateInfo()

        print('>>> Sampling job info ...')

        command = 'bjobs -u all -r -UF'
        bjobsDic = lsf_common.getBjobsUfInfo(command)
        jobList = list(bjobsDic.keys())
        jobRangeDic = common.getJobRangeDic(jobList)
        jobSqlDic = {}

        keyList = ['sampleTime', 'mem']

        for jobRange in jobRangeDic.keys():
            jobDbFile = str(self.dbPath) + '/job/' + str(jobRange) + '.db'
            (result, jobDbConn) = sqlite3_common.connectDbFile(jobDbFile,
                                                               mode='read')

            if result == 'passed':
                jobTableList = sqlite3_common.getSqlTableList(
                    jobDbFile, jobDbConn)
            else:
                jobTableList = []

            for job in jobRangeDic[jobRange]:
                jobTableName = 'job_' + str(job)

                print('    Sampling for job "' + str(job) + '" ...')

                jobSqlDic[job] = {
                    'drop': False,
                    'keyString': '',
                    'valueString': '',
                }

                # If job table (with old data) has been on the jobDbFile, drop it.
                if jobTableName in jobTableList:
                    dataDic = sqlite3_common.getSqlTableData(
                        jobDbFile, jobDbConn, jobTableName, ['sampleTime'])

                    if dataDic:
                        if len(dataDic['sampleTime']) > 0:
                            lastSampleTime = dataDic['sampleTime'][-1]
                            lastSeconds = int(
                                time.mktime(
                                    datetime.datetime.strptime(
                                        str(lastSampleTime),
                                        "%Y%m%d_%H%M%S").timetuple()))

                            if self.currentSeconds - lastSeconds > 3600:
                                common.printWarning(
                                    '    *Warning*: table "' +
                                    str(jobTableName) +
                                    '" already existed even one hour ago, will drop it.'
                                )
                                jobSqlDic[job]['drop'] = True
                                jobTableList.remove(jobTableName)

                # If job table is not on the jobDbFile, create it.
                if jobTableName not in jobTableList:
                    keyString = sqlite3_common.genSqlTableKeyString(keyList)
                    jobSqlDic[job]['keyString'] = keyString

                # Insert sql table value.
                valueList = [self.sampleTime, bjobsDic[job]['mem']]
                valueString = sqlite3_common.genSqlTableValueString(valueList)
                jobSqlDic[job]['valueString'] = valueString

            if result == 'passed':
                jobDbConn.commit()
                jobDbConn.close()

        for jobRange in jobRangeDic.keys():
            jobDbFile = str(self.dbPath) + '/job/' + str(jobRange) + '.db'
            (result, jobDbConn) = sqlite3_common.connectDbFile(jobDbFile,
                                                               mode='write')

            if result != 'passed':
                return

            for job in jobRangeDic[jobRange]:
                jobTableName = 'job_' + str(job)

                if jobSqlDic[job]['drop']:
                    sqlite3_common.dropSqlTable(jobDbFile,
                                                jobDbConn,
                                                jobTableName,
                                                commit=False)

                if jobSqlDic[job]['keyString'] != '':
                    sqlite3_common.createSqlTable(jobDbFile,
                                                  jobDbConn,
                                                  jobTableName,
                                                  jobSqlDic[job]['keyString'],
                                                  commit=False)

                if jobSqlDic[job]['valueString'] != '':
                    sqlite3_common.insertIntoSqlTable(
                        jobDbFile,
                        jobDbConn,
                        jobTableName,
                        jobSqlDic[job]['valueString'],
                        commit=False)

            jobDbConn.commit()
            jobDbConn.close()

        print('    Committing the update to sqlite3 ...')
        print('    Done (' + str(len(jobList)) + ' jobs).')