From: Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com>
To: "Jin, Yao" <yao.jin@linux.intel.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: jolsa@kernel.org, peterz@infradead.org, mingo@redhat.com,
alexander.shishkin@linux.intel.com, Linux-kernel@vger.kernel.org,
linux-perf-users@vger.kernel.org, ak@linux.intel.com,
kan.liang@intel.com, yao.jin@intel.com, rickyman7@gmail.com,
john.garry@huawei.com, Kan Liang <kan.liang@linux.intel.com>
Subject: Re: [PATCH v6 1/2] perf pmu: Add PMU alias support
Date: Wed, 01 Sep 2021 22:33:38 -0300 [thread overview]
Message-ID: <59B604C2-8E26-4B11-A9E7-F0885058D990@gmail.com> (raw)
In-Reply-To: <298a4efe-72f3-f99a-3923-939eab44885a@linux.intel.com>
On September 1, 2021 9:58:16 PM GMT-03:00, "Jin, Yao" <yao.jin@linux.intel.com> wrote:
>Hi Arnaldo,
>
>On 9/1/2021 9:57 PM, Arnaldo Carvalho de Melo wrote:
>> Em Wed, Sep 01, 2021 at 01:46:01PM +0800, Jin Yao escreveu:
>> <SNIP>
>>
>>> +++ b/tools/perf/arch/x86/util/pmu.c
>>
>> <SNIP>
>>
>>> +static int setup_pmu_alias_list(void)
>>> +{
>>> + char path[PATH_MAX];
>>> + DIR *dir;
>>> + struct dirent *dent;
>>> + const char *sysfs = sysfs__mountpoint();
>>> + struct perf_pmu_alias_name *pmu;
>>> + char buf[MAX_PMU_NAME_LEN];
>>> + FILE *file;
>>> + int ret = 0;
>>> +
>>> + if (!sysfs)
>>> + return -1;
>>> +
>>> + snprintf(path, PATH_MAX,
>>> + "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
>>> +
>>> + dir = opendir(path);
>>> + if (!dir)
>>> + return -1;
>>> +
>>> + while ((dent = readdir(dir))) {
>>> + if (!strcmp(dent->d_name, ".") ||
>>> + !strcmp(dent->d_name, ".."))
>>> + continue;
>>> +
>>> + snprintf(path, PATH_MAX,
>>> + TEMPLATE_ALIAS, sysfs, dent->d_name);
>>> +
>>> + if (!file_available(path))
>>> + continue;
>>> +
>>> + file = fopen(path, "r");
>>> + if (!file)
>>> + continue;
>>> +
>>> + if (!fgets(buf, sizeof(buf), file)) {
>>> + fclose(file);
>>> + continue;
>>> + }
>>> +
>>> + fclose(file);
>>> +
>>> + pmu = zalloc(sizeof(*pmu));
>>> + if (!pmu) {
>>> + ret = -ENOMEM;
>>> + break;
>>> + }
>>> +
>>> + /* Remove the last '\n' */
>>> + buf[strlen(buf) - 1] = 0;
>>> +
>>> + pmu->alias = strdup(buf);
>>> + if (!pmu->alias)
>>> + goto mem_err;
>>
>> This isn't returning -ENOMEM like when zalloc() fails above. Also you're
>> mixing 'return -1' with 'return -ENOMEM', please be consistent. Please
>> find some -E errno for the !sysfs case, perhaps -ENODEV?
>>
>
>For opendir() error, can we just return -errno?
>
>dir = opendir(path);
>if (!dir)
> return -errno;
Yeah
>
>>> +
>>> + pmu->name = strdup(dent->d_name);
>>> + if (!pmu->name)
>>> + goto mem_err;
>>> +
>>> + list_add_tail(&pmu->list, &pmu_alias_name_list);
>>> + continue;
>>
>>
>> Don't we have a 'struct pmu' constructor/destructor pair? I.e. instead
>> of doing all this in an open coded way as above, why not have:
>>
>> void pmu__delete(struct pmu *pmu)
>> {
>> if (!pmu)
>> return;
>>
>> zfree(&pmu->name);
>> zfree(&pmu->alias);
>> free(pmu);
>> }
>>
>> struct pmu *pmu__new(const char *name, const char *alias)
>> {
>> struct pmu *pmu = zalloc(sizeof(*pmu));
>>
>> if (pmu) {
>> pmu->name = strdup(name);
>> if (!pmu->name)
>> goto out_delete;
>>
>> pmu->alias = strdup(alias);
>> if (!pmu->alias)
>> goto out_delete;
>> }
>>
>> return pmu;
>> out_err:
>> pmu__delete(pmu);
>> return NULL;
>> }
>>
>> And then just:
>>
>> pmu = pmu__new(dent->d_name, buf);
>> if (!pmu)
>> goto out_closedir;
>>
>> list_add_tail(&pmu->list, &pmu_alias_name_list);
>>
>> And then you don't need the 'continue', as this is the end of the loop
>> block.
>>
>> That 'ret' probably should start with -ENOMEM and you end the function
>> with:
>>
>> ret = 0;
>> out_closedir:
>> closedir(dir);
>> return ret;
>> }
>
>Yes, using 'struct pmu' constructor/destructor is absolutely a good design.
>
>I will follow this approach.
I've read the other message you sent, so do the constructor/destructor for the right struct, that long named one.
- Arnaldo
next prev parent reply other threads:[~2021-09-02 1:33 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-01 5:46 [PATCH v6 0/2] perf tools: " Jin Yao
2021-09-01 5:46 ` [PATCH v6 1/2] perf pmu: " Jin Yao
2021-09-01 13:57 ` Arnaldo Carvalho de Melo
2021-09-02 0:58 ` Jin, Yao
2021-09-02 1:21 ` Jin, Yao
2021-09-02 1:33 ` Arnaldo Carvalho de Melo [this message]
2021-09-01 5:46 ` [PATCH v6 2/2] perf tests: Test for PMU alias Jin Yao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=59B604C2-8E26-4B11-A9E7-F0885058D990@gmail.com \
--to=arnaldo.melo@gmail.com \
--cc=Linux-kernel@vger.kernel.org \
--cc=acme@kernel.org \
--cc=ak@linux.intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=john.garry@huawei.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@intel.com \
--cc=kan.liang@linux.intel.com \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rickyman7@gmail.com \
--cc=yao.jin@intel.com \
--cc=yao.jin@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®