-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A software profiling library with sample program.
- Loading branch information
Showing
23 changed files
with
2,250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# | ||
# Makefile | ||
# | ||
|
||
CIRCLEHOME = ../.. | ||
|
||
OBJS = profiler.o gmon.o mcount.o profil.o arm-mcount.o glibc_compat.o | ||
|
||
libprofile.a: $(OBJS) | ||
@echo " AR $@" | ||
@rm -f $@ | ||
@$(AR) cr $@ $(OBJS) | ||
|
||
include $(CIRCLEHOME)/Rules.mk | ||
|
||
-include $(DEPS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* Implementation of profiling support. | ||
Copyright (C) 2008-2020 Free Software Foundation, Inc. | ||
This file is part of the GNU C Library. | ||
The GNU C Library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
The GNU C Library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with the GNU C Library. If not, see | ||
<https://www.gnu.org/licenses/>. */ | ||
|
||
.text | ||
|
||
#if AARCH == 32 | ||
|
||
/* Use an assembly stub with a special ABI. The calling lr has been | ||
pushed to the stack (which will be misaligned). We should preserve | ||
all registers except ip and pop a word off the stack. | ||
NOTE: This assumes mcount_internal does not clobber any non-core | ||
(coprocessor) registers. Currently this is true, but may require | ||
additional attention in the future. | ||
The calling sequence looks something like: | ||
func: | ||
push {lr} | ||
bl __gnu_mcount_nc | ||
<function body> | ||
*/ | ||
.globl __gnu_mcount_nc | ||
__gnu_mcount_nc: | ||
push {r0-r3, lr} | ||
bic r1, lr, #1 /* selfpc */ | ||
ldr r0, [sp, #20] /* frompc has been pushed to stack */ | ||
bl __mcount_internal | ||
pop {r0-r3, ip, lr} | ||
bx ip | ||
|
||
#else | ||
|
||
.globl _mcount | ||
_mcount: /* frompc is in x0 on entry */ | ||
mov x1, x30 /* selfpc */ | ||
b __mcount_internal | ||
|
||
#endif | ||
|
||
/* End */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
// | ||
// glibc_compat.cpp | ||
// | ||
// Circle - A C++ bare metal environment for Raspberry Pi | ||
// Copyright (C) 2020 R. Stange <[email protected]> | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
// | ||
#include <profile/glibc_compat.h> | ||
#include <circle/logger.h> | ||
#include <circle/util.h> | ||
#include <assert.h> | ||
|
||
#define FATFS_FILE_DESCRIPTOR 1 // only one open file supported | ||
|
||
static CFATFileSystem *s_pFileSystem = 0; | ||
|
||
static FATFS *s_pFATFS = 0; | ||
static char s_FATFSDrive[10]; | ||
static FIL *s_pFile = 0; | ||
|
||
static const char From[] = "prof"; | ||
|
||
void __set_nocancel_filesystem (CFATFileSystem *fs) | ||
{ | ||
assert (fs != 0); | ||
s_pFileSystem = fs; | ||
} | ||
|
||
void __set_nocancel_filesystem (FATFS *fs, const char *drive) | ||
{ | ||
assert (fs != 0); | ||
s_pFATFS = fs; | ||
|
||
assert (drive != 0); | ||
strncpy (s_FATFSDrive, drive, sizeof s_FATFSDrive); | ||
s_FATFSDrive[sizeof s_FATFSDrive-1] = '\0'; | ||
} | ||
|
||
int __open_nocancel (const char *name, unsigned mode, unsigned umask) | ||
{ | ||
assert (name != 0); | ||
|
||
if ((mode & (O_CREAT | O_TRUNC)) != (O_CREAT | O_TRUNC)) | ||
{ | ||
return -1; | ||
} | ||
|
||
if (s_pFileSystem != 0) | ||
{ | ||
unsigned hFile = s_pFileSystem->FileCreate (name); | ||
|
||
return hFile > 0 ? (int) hFile : -1; | ||
} | ||
else | ||
{ | ||
assert (s_pFATFS != 0); | ||
|
||
char path[80]; | ||
if (strlen (s_FATFSDrive) + strlen (name) > sizeof path-1) | ||
{ | ||
return -1; | ||
} | ||
|
||
strcpy (path, s_FATFSDrive); | ||
strcat (path, name); | ||
|
||
if (s_pFile != 0) | ||
{ | ||
return -1; | ||
} | ||
|
||
s_pFile = new FIL; | ||
assert (s_pFile != 0); | ||
|
||
if (f_open (s_pFile, path, FA_WRITE | FA_CREATE_ALWAYS) == FR_OK) | ||
{ | ||
return FATFS_FILE_DESCRIPTOR; | ||
} | ||
else | ||
{ | ||
delete s_pFile; | ||
s_pFile = 0; | ||
|
||
return -1; | ||
} | ||
} | ||
} | ||
|
||
void __writev_nocancel_nostatus (int fd, const struct iovec *vec, size_t len) | ||
{ | ||
assert (vec != 0); | ||
|
||
for (unsigned i = 0; i < len; i++) | ||
{ | ||
__write_nocancel (fd, vec[i].iov_base, vec[i].iov_len); | ||
} | ||
} | ||
|
||
void __write_nocancel (int fd, const void *buf, size_t len) | ||
{ | ||
assert (buf != 0); | ||
assert (len > 0); | ||
|
||
if (fd == STDERR_FILENO) | ||
{ | ||
char msg[100]; | ||
if (len > sizeof msg-1) | ||
{ | ||
len = sizeof msg-1; | ||
} | ||
|
||
memcpy (msg, buf, len); | ||
msg[len] = '\0'; | ||
|
||
if ( len > 1 | ||
&& msg[len-1] == '\n') | ||
{ | ||
msg[len-1] = '\0'; | ||
} | ||
|
||
CLogger::Get ()->Write (From, LogError, msg); | ||
|
||
return; | ||
} | ||
|
||
if (s_pFileSystem != 0) | ||
{ | ||
assert (len < (unsigned) -1); | ||
|
||
if (s_pFileSystem->FileWrite ((unsigned) fd, buf, (unsigned) len) != (unsigned) len) | ||
{ | ||
CLogger::Get ()->Write (From, LogError, "Write failed"); | ||
} | ||
} | ||
else | ||
{ | ||
assert (s_pFATFS != 0); | ||
|
||
unsigned byteswritten; | ||
if ( fd != FATFS_FILE_DESCRIPTOR | ||
|| s_pFile == 0 | ||
|| f_write (s_pFile, (const char *) buf, len, &byteswritten) != FR_OK | ||
|| byteswritten != len) | ||
{ | ||
CLogger::Get ()->Write (From, LogError, "Write failed"); | ||
} | ||
} | ||
} | ||
|
||
void __close_nocancel_nostatus (int fd) | ||
{ | ||
if (s_pFileSystem != 0) | ||
{ | ||
s_pFileSystem->FileClose ((unsigned) fd); | ||
} | ||
else | ||
{ | ||
assert (s_pFATFS != 0); | ||
|
||
if ( fd == FATFS_FILE_DESCRIPTOR | ||
&& s_pFile != 0) | ||
{ | ||
f_close (s_pFile); | ||
|
||
delete s_pFile; | ||
s_pFile = 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// | ||
// glibc_compat.h | ||
// | ||
// Circle - A C++ bare metal environment for Raspberry Pi | ||
// Copyright (C) 2020 R. Stange <[email protected]> | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
// | ||
#ifndef _profile_glibc_compat_h | ||
#define _profile_glibc_compat_h | ||
|
||
#include <circle/fs/fat/fatfs.h> | ||
#include <fatfs/ff.h> | ||
#include <circle/types.h> | ||
|
||
#define __BEGIN_DECLS | ||
#define __END_DECLS | ||
#define __THROW | ||
|
||
#define attribute_hidden | ||
#define libc_hidden_def(symbol) | ||
#define libc_hidden_proto(symbol) | ||
#define weak_alias(symbol, alias) | ||
|
||
#define NULL 0 | ||
|
||
typedef unsigned char u_char; | ||
typedef unsigned short u_short; | ||
typedef unsigned int u_int; | ||
typedef unsigned long u_long; | ||
|
||
struct iovec | ||
{ | ||
void *iov_base; | ||
size_t iov_len; | ||
}; | ||
|
||
#define STDERR_FILENO 0 | ||
|
||
#define O_CREAT (1 << 0) | ||
#define O_TRUNC (1 << 1) | ||
#define O_WRONLY (1 << 2) | ||
#define O_NOFOLLOW (1 << 3) | ||
|
||
void __set_nocancel_filesystem (CFATFileSystem *fs); | ||
void __set_nocancel_filesystem (FATFS *fs, const char *drive); | ||
|
||
int __open_nocancel (const char *name, unsigned mode, unsigned umask); | ||
void __writev_nocancel_nostatus (int fd, const struct iovec *vec, size_t len); | ||
void __write_nocancel (int fd, const void *buf, size_t len); | ||
void __close_nocancel_nostatus (int fd); | ||
|
||
static inline bool atomic_compare_and_exchange_bool_acq (long *mem, long newval, long oldval) | ||
{ | ||
long expected = oldval; | ||
|
||
return !__atomic_compare_exchange_n (mem, &expected, newval, false, | ||
#ifdef ARM_ALLOW_MULTI_CORE | ||
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST | ||
#else | ||
__ATOMIC_RELAXED, __ATOMIC_RELAXED | ||
#endif | ||
); | ||
} | ||
|
||
static inline int ffs (int i) | ||
{ | ||
int mask = 1; | ||
for (int n = 1; n <= 32; n++, mask <<= 1) | ||
{ | ||
if (i & mask) | ||
{ | ||
return n; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
#endif |
Oops, something went wrong.