【PyCharm Community Edition】:分析map文件统计RAM及ROM的使用

本文介绍了在编译器不便统计代码的RAM和ROM使用时,如何利用PyCharm进行.map文件分析。通过WalkFiles.py遍历.map文件,AnalyzerMap模块进行统计,再由AnalyzerOutput.py输出Excel报告,详细解析了整个过程。
摘要由CSDN通过智能技术生成

前序

当我们的编译器不方便统计代码的RAM及ROM的使用总数量时,我们就需要分析.map文件,手动统计其具体使用量。

文件遍历

当我们需要选择不同文件做相同的逻辑处理时,我们就会用到一种遍历的方法,来获取需要被选择的文件(比如.map文件);在python标准库中遍历文档主要是用到的方法:walk()。

涉及函数:

  • walk
    在这里插入图片描述

WalkFiles.py

import os,sys
#########################################################################################
# walk  依赖的参数解释:
# root  表示当前正在访问的文件夹路径
# dirs  表示该文件夹下的子目录名list
# files 表示该文件夹下的文件list

def WalkFiles_FindMap(filePath=".\map"):               #可以在这里指定路径
    allfiles = []
    for root, dirs, files in os.walk(filePath):
        for f in files:
            #print("files",os.path.join(root, f))
            fname = os.path.join(root, f)
            if fname[-4:] == ".map":                    #只查找后缀名为.map文件
                allfiles.append(os.path.join(root, f))  #查找到后添加到数组中
        for d in dirs:
            WalkFiles_FindMap(os.path.join(root, d))            #递归
            #print("dirs",os.path.join(root, d))
    return allfiles

def WalkFiles_SelectFile():
    files = WalkFiles_FindMap()
    if len(files) < 1:
        return None
    else:
        selected = 0
        if len(files) > 1:
            no = 0
            for file in files:
                print("%d:   %s" % (no,os.path.abspath(file)))
                no =no +1
            selected = int(input("请选择哪个文件?\r\n_____"))
            if (selected >= len(files)) or (selected < 0):
                print("无效的输入")
                return None
        print("您选择的是:",os.path.abspath(files[selected]))
        return os.path.abspath(files[selected])#返回绝对路径
#########################################################################################

统计方法

在单片机中:

  • ROM地址区间一般是0x0-------开始
  • RAM地址区间一般是0xf-------开始

涉及函数:

  • open(文件打开)
  • split(分割函数)
  • int(转换成16进制)

AnalyzerMap_Cfg.py

class global_var:
    Image_Summary = "Image Summary"
    Module_Summary = "Module Summary"
    Global_Symbols = "Global Symbols"

    RAM_BlockSizeLargerThanPrintfEn = 1 #1:打印使能
    RAM_BlockSizeLargerThan = 4*1024#单个长度超过此值时决定打印

AnalyzerMap.py

from ColorInfo import ColorLogger
import AnalyzerMap_Cfg as map_cfg
import openpyxl
import os
import xlrd

logger = ColorLogger()

class tMcuFlashInfo():
    num = 0
    singleInfoByDict = {
   }

class tMcuFlashSingleInfo():
    name = ''
    startAdd = ''
    endAdd = ''
    usedLen = 0
    totalLen = 1

def addessJudge(line,address,length,tMcuFlashInfo):
    i = 0
    while i < tMcuFlashInfo.num:
        address_min = tMcuFlashInfo.singleInfoByDict[0][i].startAdd
        address_max = tMcuFlashInfo.singleInfoByDict[0][i].endAdd
        if int(address, 16) >= int(address_min, 16) and int(address, 16) <= int(address_max, 16):
            if -1 == line.find(".debug"):  # 去除debug信息
                tMcuFlashInfo.singleInfoByDict[
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Autosar️研究员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值