I am new to OpenBSD. I have worked on Linux before. I am looking for the directory where I can find the information about the processes running currently. In Linux, we have /proc directory where the entire list is present. But I can not find a similar setup in OpenBSD 4.6. I know there are commands like ps, top and sysctl but I want to get that information through a C code.
procfs in the BSDs is either deprecated or removed altogether, sorry.
That being said, it's also quite usual to have the sources for your system under /usr/src, so you can look at them if you really need to.
Or you can just browse them on the web, eg http://bxr.su/o/bin/ps/ps.c
You can use sysctl to get the running processes in an array of kinfo_proc structures, this type is defined in:
/usr/include/sys/sysctl.h
The top command uses a function named getprocs that works this way, it's defined in:
/usr/src/usr.bin/top/machine.c
The next utility outputs information of all running processes using a slightly modified version of getprocs:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <kvm.h>
#include <sys/sysctl.h>
#define TRUE 1
#define FALSE 0
struct kinfo_proc * getprocs( int * count, int threads )
{
struct kinfo_proc * procbase = NULL ;
unsigned int maxslp ;
size_t size = sizeof( maxslp ) ;
int maxslp_mib[] = { CTL_VM, VM_MAXSLP } ;
int mib[6] =
{
CTL_KERN,
KERN_PROC,
threads ? KERN_PROC_KTHREAD | KERN_PROC_SHOW_THREADS : KERN_PROC_KTHREAD,
0,
sizeof( struct kinfo_proc ),
0
} ;
if( sysctl( maxslp_mib, 2, &maxslp, &size, NULL, 0 ) == -1 )
{
perror( "list" ) ;
return NULL ;
}
retry:
if( sysctl( mib, 6, NULL, &size, NULL, 0 ) == -1 )
{
perror( "list" ) ;
return NULL ;
}
size = 5 * size / 4 ; /* extra slop */
procbase = (struct kinfo_proc *)malloc( size ) ;
if( procbase == NULL )
{
perror( "list" ) ;
return NULL ;
}
mib[5] = (int)( size / sizeof( struct kinfo_proc ) ) ;
if( sysctl( mib, 6, procbase, &size, NULL, 0 ) )
{
if( errno == ENOMEM )
{
free( procbase ) ;
goto retry;
}
perror( "list" ) ;
return NULL ;
}
*count = (int)( size / sizeof( struct kinfo_proc ) ) ;
return procbase ;
}
int showinfo( int threads )
{
struct kinfo_proc * list, * proc ;
int count, i ;
if( ( list = getprocs( &count, threads ) ) == NULL )
{
return 1 ;
}
proc = list ;
if( threads )
{
for( i = 0 ; i < count ; ++i, ++proc )
{
if( proc->p_tid != -1 )
{
printf( "%s: pid: %d (tid: %d)\n", proc->p_comm, proc->p_pid, proc->p_tid ) ;
}
}
}
else
{
for( i = 0 ; i < count ; ++i, ++proc )
{
printf( "%s: pid: %d\n", proc->p_comm, proc->p_pid ) ;
}
}
return 0 ;
}
int main( int argc, char * argv[] )
{
if( argc == 1 )
{
return showinfo( FALSE ) ;
}
else if( argc == 2 && ( !strcmp( argv[1], "-t" ) || !strcmp( argv[1], "--threads" ) ) )
{
return showinfo( TRUE ) ;
}
else
{
printf( "Usage:\n" ) ;
printf( " list [-h] [-t]\n\n" ) ;
printf( "Options:\n" ) ;
printf( " -h, --help Print this information\n" ) ;
printf( " -t, --threads Show threads\n\n" ) ;
return 0 ;
}
}
Related
I am using RISC-V 32E toolchain to compile some standalone CPP code. I got errors below.
/opt/riscv32i/bin/riscv32-unknown-elf-g++ -Os -ffreestanding -o firmware/firmware.elf \
-Wl,-Bstatic,-T,firmware/sections.lds,-Map,firmware/firmware.map,--strip-debug \
firmware/start.o firmware/print.o firmware/stream.o firmware/main.o firmware/accel.o firmware/data_redir_m.o -lgcc -fno-threadsafe-statics -nostdlib
/opt/riscv32i/lib/gcc/riscv32-unknown-elf/8.2.0/../../../../riscv32-unknown-elf/bin/ld: firmware/data_redir_m.o: in function `.L31':
data_redir_m.cpp:(.text+0x442): undefined reference to `memcpy'
collect2: error: ld returned 1 exit status
Makefile:46: recipe for target 'firmware/firmware.elf' failed
make: *** [firmware/firmware.elf] Error 1
I want to keep the hex file as small as possible, so my preference is to not use -lstdc++ library. What confused me a lot is why it still complains about such errors even if I manually add the memcpy() function definition in data_redir_m.cpp file.
#include "typedefs.h"
void * memcpy ( void * destination, const void * source, int num ){
int i=0;
*((int*)destination) = *((int*)source);
}
static int check_clockwise( Triangle_2D triangle_2d )
{
int cw;
cw = (triangle_2d.x2 - triangle_2d.x0) * (triangle_2d.y1 - triangle_2d.y0)
- (triangle_2d.y2 - triangle_2d.y0) * (triangle_2d.x1 - triangle_2d.x0);
return cw;
}
// swap (x0, y0) (x1, y1) of a Triangle_2D
static void clockwise_vertices( Triangle_2D *triangle_2d )
{
bit8 tmp_x, tmp_y;
tmp_x = triangle_2d->x0;
tmp_y = triangle_2d->y0;
triangle_2d->x0 = triangle_2d->x1;
triangle_2d->y0 = triangle_2d->y1;
triangle_2d->x1 = tmp_x;
triangle_2d->y1 = tmp_y;
}
// find the min from 3 integers
static bit8 find_min( bit8 in0, bit8 in1, bit8 in2 )
{
if (in0 < in1)
{
if (in0 < in2)
return in0;
else
return in2;
}
else
{
if (in1 < in2)
return in1;
else
return in2;
}
}
// find the max from 3 integers
static bit8 find_max( bit8 in0, bit8 in1, bit8 in2 )
{
if (in0 > in1)
{
if (in0 > in2)
return in0;
else
return in2;
}
else
{
if (in1 > in2)
return in1;
else
return in2;
}
}
// project a 3D triangle to a 2D triangle
void projection(
bit32 input_lo,
bit32 input_mi,
bit32 input_hi,
Triangle_2D *triangle_2d
)
{
#pragma HLS INLINE off
Triangle_3D triangle_3d;
// Setting camera to (0,0,-1), the canvas at z=0 plane
// The 3D model lies in z>0 space
// The coordinate on canvas is proportional to the corresponding coordinate
// on space
bit2 angle = 0;
triangle_3d.x0 = bit8(input_lo( 7, 0));
triangle_3d.y0 = bit8(input_lo(15, 8));
triangle_3d.z0 = bit8(input_lo(23, 16));
triangle_3d.x1 = bit8(input_lo(31, 24));
triangle_3d.y1 = bit8(input_mi( 7, 0));
triangle_3d.z1 = bit8(input_mi(15, 8));
triangle_3d.x2 = bit8(input_mi(23, 16));
triangle_3d.y2 = bit8(input_mi(31, 24));
triangle_3d.z2 = bit8(input_hi( 7, 0));
if(angle == 0)
{
triangle_2d->x0 = triangle_3d.x0;
triangle_2d->y0 = triangle_3d.y0;
triangle_2d->x1 = triangle_3d.x1;
triangle_2d->y1 = triangle_3d.y1;
triangle_2d->x2 = triangle_3d.x2;
triangle_2d->y2 = triangle_3d.y2;
triangle_2d->z = triangle_3d.z0 / 3 + triangle_3d.z1 / 3 + triangle_3d.z2 / 3;
}
else if(angle == 1)
{
triangle_2d->x0 = triangle_3d.x0;
triangle_2d->y0 = triangle_3d.z0;
triangle_2d->x1 = triangle_3d.x1;
triangle_2d->y1 = triangle_3d.z1;
triangle_2d->x2 = triangle_3d.x2;
triangle_2d->y2 = triangle_3d.z2;
triangle_2d->z = triangle_3d.y0 / 3 + triangle_3d.y1 / 3 + triangle_3d.y2 / 3;
}
else if(angle == 2)
{
triangle_2d->x0 = triangle_3d.z0;
triangle_2d->y0 = triangle_3d.y0;
triangle_2d->x1 = triangle_3d.z1;
triangle_2d->y1 = triangle_3d.y1;
triangle_2d->x2 = triangle_3d.z2;
triangle_2d->y2 = triangle_3d.y2;
triangle_2d->z = triangle_3d.x0 / 3 + triangle_3d.x1 / 3 + triangle_3d.x2 / 3;
}
}
// calculate bounding box for a 2D triangle
void rasterization1 (
Triangle_2D triangle_2d,
hls::stream<ap_uint<32> > & Output_1,
hls::stream<ap_uint<32> > & Output_2
)
{
Triangle_2D triangle_2d_same;
bit8 max_min[5];
max_min[0]=0;
max_min[1]=0;
max_min[2]=0;
max_min[3]=0;
max_min[4]=0;
bit16 max_index[1];
max_index[0]=0;
bit32 tmp1, tmp2, tmp3, tmp4;
static int parity = 0;
#pragma HLS INLINE off
// clockwise the vertices of input 2d triangle
if ( check_clockwise( triangle_2d ) == 0 ){
tmp1(7,0) = 1;
tmp1(15, 8) = triangle_2d_same.x0;
tmp1(23,16) = triangle_2d_same.y0;
tmp1(31,24) = triangle_2d_same.x1;
tmp2(7,0) = triangle_2d_same.y1;
tmp2(15, 8) = triangle_2d_same.x2;
tmp2(23,16) = triangle_2d_same.y2;
tmp2(31,24) = triangle_2d_same.z;
tmp3(15,0) = max_index[0];
tmp3(23,16) = max_min[0];
tmp3(31,24) = max_min[1];
tmp4(7,0) = max_min[2];
tmp4(15, 8) = max_min[3];
tmp4(23,16) = max_min[4];
tmp4(31,24) = 0;
if(parity==0){
Output_1.write(tmp1);
Output_1.write(tmp2);
Output_1.write(tmp3);
Output_1.write(tmp4);
parity = 1;
}else{
Output_2.write(tmp1);
Output_2.write(tmp2);
Output_2.write(tmp3);
Output_2.write(tmp4);
parity = 0;
}
#ifdef PROFILE
data_redir_m_out_1+=4;
#endif
return;
}
if ( check_clockwise( triangle_2d ) < 0 )
clockwise_vertices( &triangle_2d );
// copy the same 2D triangle
triangle_2d_same.x0 = triangle_2d.x0;
triangle_2d_same.y0 = triangle_2d.y0;
triangle_2d_same.x1 = triangle_2d.x1;
triangle_2d_same.y1 = triangle_2d.y1;
triangle_2d_same.x2 = triangle_2d.x2;
triangle_2d_same.y2 = triangle_2d.y2;
triangle_2d_same.z = triangle_2d.z ;
// find the rectangle bounds of 2D triangles
max_min[0] = find_min( triangle_2d.x0, triangle_2d.x1, triangle_2d.x2 );
max_min[1] = find_max( triangle_2d.x0, triangle_2d.x1, triangle_2d.x2 );
max_min[2] = find_min( triangle_2d.y0, triangle_2d.y1, triangle_2d.y2 );
max_min[3] = find_max( triangle_2d.y0, triangle_2d.y1, triangle_2d.y2 );
max_min[4] = max_min[1] - max_min[0];
// calculate index for searching pixels
max_index[0] = (max_min[1] - max_min[0]) * (max_min[3] - max_min[2]);
tmp1(7,0) = 0;
tmp1(15,8) = triangle_2d_same.x0;
tmp1(23,16) = triangle_2d_same.y0;
tmp1(31,24) = triangle_2d_same.x1;
tmp2(7,0) = triangle_2d_same.y1;
tmp2(15,8) = triangle_2d_same.x2;
tmp2(23,16) = triangle_2d_same.y2;
tmp2(31,24) = triangle_2d_same.z;
tmp3(15,0) = max_index[0];
tmp3(23,16) = max_min[0];
tmp3(31,24) = max_min[1];
tmp4(7,0) = max_min[2];
tmp4(15,8) = max_min[3];
tmp4(23, 16) = max_min[4];
tmp4(31, 24) = 0;
if(parity==0){
Output_1.write(tmp1);
Output_1.write(tmp2);
Output_1.write(tmp3);
Output_1.write(tmp4);
parity = 1;
}else{
Output_2.write(tmp1);
Output_2.write(tmp2);
Output_2.write(tmp3);
Output_2.write(tmp4);
parity = 0;
}
return;
}
void data_redir_m (
hls::stream<ap_uint<32> > & Input_1,
hls::stream<ap_uint<32> > & Output_1,
hls::stream<ap_uint<32> > & Output_2
)
{
#pragma HLS INTERFACE ap_hs port=Input_1
#pragma HLS INTERFACE ap_hs port=Output_1
#pragma HLS INTERFACE ap_hs port=Output_2
bit32 input_lo;
bit32 input_mi;
bit32 input_hi;
bit128 input_tmp;
hls::stream<ap_uint<32> > Output_1_1;
hls::stream<ap_uint<32> > Output_2_2;
Triangle_2D triangle_2ds_1;
Triangle_2D triangle_2ds_2;
input_lo = Input_1.read();
input_mi = Input_1.read();
input_hi = Input_1.read();
#ifdef PROFILE
data_redir_m_in_1+=3;
#endif
projection (input_lo,input_mi,input_hi,&triangle_2ds_1);
rasterization1 (triangle_2ds_1, Output_1, Output_2);
}
If you want to build gcc for non-os environment, you need to build newlib in advance. And tell gcc where to find the libc, the way is to declare sysroot when configure the project.
You can use this script(ian910297/build-riscv-gnu-toolchain) to build riscv gnu toolchain. If you choose this method, be care of the directory name.
Otherwise, riscv official also provide similar script to build, like: riscv/riscv-gnu-toolchain)
I am the novice in the field of distributed-computation and I know the most popular standard is the Message Passing Interface. However, if I only have one server, I can also run my program under the MPI framework as the following demo example.
# include <cmath>
# include <cstdlib>
# include <ctime>
# include <iomanip>
# include <iostream>
# include <mpi.h>
using namespace std;
int main ( int argc, char *argv[] );
double f ( double x );
void timestamp ( );
int main ( int argc, char *argv[] )
{
double end_time;
int i;
int id;
int ierr;
int m;
int p;
double r8_pi = 3.141592653589793238462643;
int process;
double q_global;
double q_local;
int received;
int source;
double start_time;
MPI_Status status;
int tag;
int target;
double x;
double xb[2];
double x_max = 1.0;
double x_min = 0.0;
//
// Establish the MPI environment.
//
ierr = MPI_Init ( &argc, &argv );
if ( ierr != 0 )
{
cout << "\n";
cout << "INTERVALS_MPI - Fatal error!";
cout << " MPI_Init returned ierr = " << ierr << "\n";
exit ( 1 );
}
//
// Determine this processes's rank.
//
ierr = MPI_Comm_rank ( MPI_COMM_WORLD, &id );
//
// Get the number of processes.
//
ierr = MPI_Comm_size ( MPI_COMM_WORLD, &p );
//
// Say hello (once), and shut down right away unless we
// have at least 2 processes available.
//
if ( id == 0 )
{
timestamp ( );
cout << "\n";
cout << "INTERVALS - Master process:\n";
cout << " C++ version\n";
cout << "\n";
cout << " An MPI example program,\n";
cout << " A quadrature over an interval is done by\n";
cout << " assigning subintervals to processes.\n";
cout << "\n";
cout << " The number of processes is " << p << "\n";
start_time = MPI_Wtime ( );
if ( p <= 1 )
{
cout << "\n";
cout << "INTERVALS - Master process:\n";
cout << " Need at least 2 processes!\n";
MPI_Finalize ( );
cout << "\n";
cout << "INTERVALS - Master process:\n";
cout << " Abnormal end of execution.\n";
exit ( 1 );
}
}
cout << "\n";
cout << "Process " << id << ": Active!\n";
//
// Every process could figure out the endpoints of its interval
// on its own. But we want to demonstrate communication. So we
// assume that the assignment of processes to intervals is done
// only by the master process, which then tells each process
// what job it is to do.
//
if ( id == 0 )
{
for ( process = 1; process <= p-1; process++ )
{
xb[0] = ( ( double ) ( p - process ) * x_min
+ ( double ) ( process - 1 ) * x_max )
/ ( double ) ( p - 1 );
xb[1] = ( ( double ) ( p - process - 1 ) * x_min
+ ( double ) ( process ) * x_max )
/ ( double ) ( p - 1 );
target = process;
tag = 1;
ierr = MPI_Send ( xb, 2, MPI_DOUBLE, target, tag, MPI_COMM_WORLD );
}
}
else
{
source = 0;
tag = 1;
ierr = MPI_Recv ( xb, 2, MPI_DOUBLE, source, tag, MPI_COMM_WORLD, &status );
}
//
// Wait here until everyone has gotten their assignment.
//
ierr = MPI_Barrier ( MPI_COMM_WORLD );
if ( id == 0 )
{
cout << "\n";
cout << "INTERVALS - Master process:\n";
cout << " Subintervals have been assigned.\n";
}
//
// Every process needs to be told the number of points to use.
// Since this is the same value for everybody, we use a broadcast.
// Again, we are doing it in this roundabout way to emphasize that
// the choice for M could really be made at runtime, by processor 0,
// and then sent out to the others.
//
m = 100;
source = 0;
ierr = MPI_Bcast ( &m, 1, MPI_INT, source, MPI_COMM_WORLD );
//
// Now, every process EXCEPT 0 computes its estimate of the
// integral over its subinterval, and sends the result back
// to process 0.
//
if ( id != 0 )
{
q_local = 0.0;
for ( i = 1; i <= m; i++ )
{
x = ( ( double ) ( 2 * m - 2 * i + 1 ) * xb[0]
+ ( double ) ( 2 * i - 1 ) * xb[1] )
/ ( double ) ( 2 * m );
q_local = q_local + f ( x );
}
q_local = q_local * ( xb[1] - xb[0] ) / ( double ) ( m );
target = 0;
tag = 2;
ierr = MPI_Send ( &q_local, 1, MPI_DOUBLE, target, tag, MPI_COMM_WORLD );
}
//
// Process 0 expects to receive N-1 partial results.
//
else
{
received = 0;
q_global = 0.0;
while ( received < p - 1 )
{
source = MPI_ANY_SOURCE;
tag = 2;
ierr = MPI_Recv ( &q_local, 1, MPI_DOUBLE, source, tag, MPI_COMM_WORLD,
&status );
q_global = q_global + q_local;
received = received + 1;
}
}
//
// The master process prints the answer.
//
if ( id == 0 )
{
cout << "\n";
cout << "INTERVALS - Master process:\n";
cout << " Estimate for PI is " << q_global << "\n";
cout << " Error is " << q_global - r8_pi << "\n";
end_time = MPI_Wtime ( );
cout << "\n";
cout << " Elapsed wall clock seconds = "
<< end_time - start_time << "\n";
}
//
// Terminate MPI.
//
MPI_Finalize ( );
//
// Terminate.
//
if ( id == 0 )
{
cout << "\n";
cout << "INTERVALS - Master process:\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
}
return 0;
}
//****************************************************************************80
double f ( double x )
{
double value;
value = 4.0 / ( 1.0 + x * x );
return value;
}
//****************************************************************************80
void timestamp ( )
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct std::tm *tm_ptr;
std::time_t now;
now = std::time ( NULL );
tm_ptr = std::localtime ( &now );
std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr );
std::cout << time_buffer << "\n";
return;
# undef TIME_SIZE
}
Actually, this is the simple case that we use the MPI to compute the integral of specific function. I run this program by using 4 processes. I am confused that we can also use the OpenMP to do the share memory programming instead of MPI to reduce the communication cost. I do not know the meaning of MPI on single machine.
I'm writing a program which reads MPU9250's accelerometer&gyro data using MPU9250's internal FIFO and serves web interface.
Without web access, everything is fine.
However, on the exact timing of web request, bytes read on that timing is changed.
My current code just simply shows template webpage and prints IMU accelerometer value only if its magnitude is greater than 2.5. So, if I do not touch MPU9250 sensor, its value should be between 0.9~1.1. However, it prints some wrong values greater than 2.5 when the webpage is refreshed (currently auto-refreshing in 0.5 seconds ) although MPU9250 sensor is not touched at all.
I'm using LOLIN D32 PRO board. and MPU9250 is connected its default VSPI bus.
I read MPU 9250's FIFO data on arduino's loop function as follows,
while( readBytes = spiread_fifo( buffer + readBytes_all / 2 ) )
readBytes_all += readBytes;
if ( readBytes_all == 0 )
return;
if( digitalRead( 4 ) == 1 ){
int x = buffer[ 0 ];
int y = buffer[ 1 ];
int z = buffer[ 2 ];
double m = sqrt( x * x + y * y + z * z ) / 2048;
if( m > 2.5 )
Serial.println( m );
and spiread_fifo is defined as follows
int spiread_fifo( volatile short * buffer ) {
int fifo_len = spiread( 0x72 ) * 256 + spiread( 0x73 );
if ( fifo_len > 512 )
return -1;
if ( fifo_len == 0 )
return 0;
MPU9250.beginTransaction( settingsB );
digitalWrite( SS, LOW );
MPU9250.transfer( 0x74 | 0x80 );
MPU9250.transfer( 0x00 ); // if I use SPI CLOCK more than 8~12Mhz, it gives me duplicated byte at the beginning. So just drop one of them.
for ( int i = 0; i < 3; i++ )
buffer[ i ] = MPU9250.transfer16( 0x00 );
for ( int i = 3; i < fifo_len / 2; i++ )
MPU9250.transfer16( 0x00 );
digitalWrite( SS, HIGH );
MPU9250.endTransaction();
for ( int i = 0; i < 12; i++ )
__asm__ __volatile__ ("nop\n\t");
return fifo_len;
}
If I do not touch or exert any force on MPU9250, serial console should be silent and it actually does if there's no ESP32's webserber access. However it gives some random value on the timing of webpage access, like follows. ( single value per single webpage access )
15.53
16.11
15.60
13.59
16.86
2.55
2.55
3.85
3.85
3.37
6.79
2.63
2.56
5.80
10.18
5.88
3.65
5.80
5.48
2.95
4.01
4.01
3.10
2.90
3.17
9.31
14.97
7.08
16.29
which are totally abnormal values.
My guesses are,
strong RF signal affects SPI bus so signals are altered.
wifi TX routine is called during MPU9250 FIFO readout process and it causes data drop during FIFO read.
Whatever the reason is, I don't know how to fix the problem.
Any possible causes/solutions would be appreciated.
Belows are my current wiring. However, nothing's special. MPU9250 is connected to default VSPI port and INT pin is connected to GPIO34. other remaining connection is not used.
PICTURE 1
PICTURE 2
full source codes below for your reference.
MAIN CODE :
extern volatile int cnt;
volatile short* buffer;
volatile short* buffer2;
volatile unsigned long *timestamp;
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
void printHex( int num, int precision) {
char tmp[16];
char format[128];
sprintf(format, "%%.%dX ", precision);
sprintf(tmp, format, num);
if ( strlen( tmp ) > precision + 1 ) {
int l = strlen( tmp ) - precision - 1;
for ( int i = 0; i < precision + 2; i++ ) {
tmp[ i ] = tmp[ i + l ];
}
}
Serial.print(tmp);
}
void setup() {
Serial.begin( 2000000 );
Serial.println( "Turning on...." );
buffer = ( short * )ps_malloc( 1000000 );
buffer2 = ( short * )ps_malloc( 1000000 );
BUTTONSetup();
MPU9250Setup();
OTASetup();
WEBSERVERSetup();
Serial.println( "Setup finished." );
}
void loop() {
OTAHandle();
WEBSERVERHandle();
int readBytes = 0, readBytes_all = 0;
while( readBytes = spiread_fifo( buffer + readBytes_all / 2 ) )
readBytes_all += readBytes;
if ( readBytes_all == 0 )
return;
if( digitalRead( 4 ) == 1 ){
int x = buffer[ 0 ];
int y = buffer[ 1 ];
int z = buffer[ 2 ];
double m = sqrt( x * x + y * y + z * z ) / 2048;
if( m > 2.5 )
Serial.println( m );
BUTTONHandle();
}
}
MPU9250 CODE :
#include <SPI.h>
#define SCK 18
#define MISO 19
#define MOSI 23
#define SS 5
#define INT 34
SPIClass MPU9250( VSPI );
SPISettings settingsA( 1000000, MSBFIRST, SPI_MODE3 );
SPISettings settingsB( 20000000, MSBFIRST, SPI_MODE3 );
volatile int cnt = 0;
void IRAM_ATTR onInterrupt() {
portENTER_CRITICAL_ISR(&mux);
cnt++;
portEXIT_CRITICAL_ISR(&mux);
}
void spiwrite( byte a, byte b ) {
MPU9250.beginTransaction( settingsA );
digitalWrite( SS, LOW );
MPU9250.transfer( a );
MPU9250.transfer( b );
digitalWrite( SS, HIGH );
MPU9250.endTransaction();
}
byte spiread( byte a ) {
MPU9250.beginTransaction( settingsB );
digitalWrite( SS, LOW );
MPU9250.transfer( a | 0x80 );
byte r = MPU9250.transfer( 0x00 );
digitalWrite( SS, HIGH );
MPU9250.endTransaction();
return r;
}
int spiread_fifo( volatile short * buffer ) {
int fifo_len = spiread( 0x72 ) * 256 + spiread( 0x73 );
// fifo_len += 12;
// fifo_len = fifo_len / 12 * 12;
if ( fifo_len > 512 )
return -1;
if ( fifo_len == 0 )
return 0;
MPU9250.beginTransaction( settingsB );
digitalWrite( SS, LOW );
MPU9250.transfer( 0x74 | 0x80 );
MPU9250.transfer( 0x00 ); // if I use SPI CLOCK more than 8~12Mhz, it gives me duplicated byte at the beginning. So just drop one of them.
for ( int i = 0; i < 3; i++ )
buffer[ i ] = MPU9250.transfer16( 0x00 );
for ( int i = 3; i < fifo_len / 2; i++ )
MPU9250.transfer16( 0x00 );
// for( int i = fifo_len / 2 + 1; i < fifo_len; i++ )
// buffer[ i ] = 0;
digitalWrite( SS, HIGH );
MPU9250.endTransaction();
for ( int i = 0; i < 12; i++ )
__asm__ __volatile__ ("nop\n\t");
return fifo_len;
}
void spiread_raw( volatile short * buffer ) {
MPU9250.beginTransaction( settingsB );
digitalWrite( SS, LOW );
MPU9250.transfer( 0x3b | 0x80 );
for ( int i = 0; i < 3; i++ )
buffer[ i ] = MPU9250.transfer16( 0x00 );
digitalWrite( SS, HIGH );
MPU9250.endTransaction();
for ( int i = 0; i < 12; i++ )
__asm__ __volatile__ ("nop\n\t");
}
void spiread_raw_gyr( volatile short * buffer ) {
MPU9250.beginTransaction( settingsB );
digitalWrite( SS, LOW );
MPU9250.transfer( 0x43 | 0x80 );
for ( int i = 0; i < 3; i++ )
buffer[ i ] = MPU9250.transfer16( 0x00 );
digitalWrite( SS, HIGH );
MPU9250.endTransaction();
for ( int i = 0; i < 12; i++ )
__asm__ __volatile__ ("nop\n\t");
}
void spiread_raw_accgyr( volatile short * buffer ) {
MPU9250.beginTransaction( settingsB );
digitalWrite( SS, LOW );
MPU9250.transfer( 0x3b | 0x80 );
for ( int i = 0; i < 3; i++ )
buffer[ i ] = MPU9250.transfer16( 0x00 );
MPU9250.transfer16( 0x00 );
for ( int i = 3; i < 6; i++ )
buffer[ i ] = MPU9250.transfer16( 0x00 );
digitalWrite( SS, HIGH );
MPU9250.endTransaction();
for ( int i = 0; i < 12; i++ )
__asm__ __volatile__ ("nop\n\t");
}
void MPU9250Setup(){
pinMode( SS, OUTPUT );
pinMode( SCK, OUTPUT );
pinMode( MOSI, OUTPUT );
pinMode( INT, INPUT_PULLUP );
pinMode( MISO, INPUT );
pinMode( 4, INPUT );
MPU9250.begin( SCK, MISO, MOSI, SS ); //CLK,MISO,MOIS,SS
attachInterrupt( digitalPinToInterrupt( INT ), onInterrupt, FALLING );
spiwrite( 0x68, 0x07 );
spiwrite( 0x6A, 0x55 ); // FIFO_EN = 1, FIFO_RST = 1;
spiwrite( 0x19, 0x00 ); // SMPLRT_DIV = 0
spiwrite( 0x1B, 0x18 ); // GYRO_FS_SEL = 3, Fchoice_b = 0
spiwrite( 0x1C, 0x18 ); // ACCEL_FS_SEL = 3
spiwrite( 0x1D, 0x08 ); // accel_fchoice_b = 1
// spiwrite( 0x23, 0x78 ); // TEMP_OUT = 0, GYRO_XOUT = 1, GYRO_YOUT = 1, GYRO_ZOUT = 1, ACCEL = 1
// spiwrite( 0x23, 0x79 ); // TEMP_OUT = 0, GYRO_XOUT = 0, GYRO_YOUT = 0, GYRO_ZOUT = 0, ACCEL = 1
spiwrite( 0x23, 0x08 ); // TEMP_OUT = 0, GYRO_XOUT = 0, GYRO_YOUT = 0, GYRO_ZOUT = 0, ACCEL = 1
spiwrite( 0x37, 0x10 ); // INT_ANYRD_2CLEAR = 1
spiwrite( 0x38, 0xC1 ); // ACTL = 1, OPEN = 1, RAW_RDY_EN = 1
spiwrite( 0x1A, 0x07 ); // FIFO_MODE = 0, EXT_SYNC_SET = 0, DLPF_CFG = 7
}
OTA firmware CODE :
#include <WiFi.h>
#include "esp_wifi.h"
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
const char* ssid = "XXXXXX";
const char* password = "XXXXXX";
void OTASetup(){
delay( 100 );
esp_wifi_set_max_tx_power( -100 );
WiFi.mode( WIFI_STA );
WiFi.setHostname( "LOLIN_D32_PRO_Sunkyue" );
delay( 100 );
WiFi.begin( ssid, password );
while( WiFi.waitForConnectResult() != WL_CONNECTED ){
Serial.println( "Connection Failed! Rebooting..." );
delay( 10 );
ESP.restart();
}
ArduinoOTA.setPort(53232);
ArduinoOTA.setHostname("LOLIN_D32_PRO_Sunkyue");
ArduinoOTA.setPassword("XXXXXX");
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void OTAHandle(){
ArduinoOTA.handle();
}
WEBSERVER CODE :
#include <WiFiClient.h>
#include <WebServer.h>
WebServer server(80);
void handleRoot() {
char temp[400];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
snprintf(temp, 400,
"<html>\
<head>\
<meta http-equiv='refresh' content='0.5'/>\
<title>ESP32 Demo</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>Hello from ESP32!</h1>\
<p>Uptime: %02d:%02d:%02d</p>\
<img src=\"/test.svg\" />\
</body>\
</html>",
hr, min % 60, sec % 60
);
server.send(200, "text/html", temp);
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void drawGraph() {
String out = "";
char temp[100];
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n";
out += "<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
out += "<g stroke=\"black\">\n";
int y = rand() % 130;
for (int x = 10; x < 390; x += 10) {
int y2 = rand() % 130;
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2);
out += temp;
y = y2;
}
out += "</g>\n</svg>\n";
server.send(200, "image/svg+xml", out);
}
void WEBSERVERSetup(){
if (MDNS.begin("esp32")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/test.svg", drawGraph);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
}
void WEBSERVERHandle(){
server.handleClient();
}
I can see two solutions for a uni-processor solution(since you have not stated the final goals one may be more optimal than the other):
If it is ok to miss approximately 15-25 ms of data(a tap of the sensor should still register,) Change the spiread_fifo(...) to spiread_raw_accgyr(buffer) in the loop(). This will read the current values into the buffer at the time of the execution. This would definitely not be appropriate for position calculations or calculations of seismic events.
Read and analyze the required data in an interrupt routine(either the hardware pin from the MPU9250, or a timer), since the web client is much more forgiving of delays(within reason).
Either way, the calculations should be optimized to reduce the instruction count(the ESP32 does not have an FPU, so all of the floating point operations must be emulated in software):
The lines:
double m = sqrt( x * x + y * y + z * z ) / 2048;
if( m > 2.5 )
Can be simplified (if perfect (spherical) accuracy is required) by algebraically squaring both sides of the equation to:
double m = (x * x + y * y + z * z) / 4194304;
if (m > 6.25)
Or(preferably, but with slightly less accuracy when combining the axes):
double m = ((abs(x) + abs(y) + abs(z)) / 2048); // Manhattan distance(a diamond in 2D, a four sided, double pyramid in 3d)
if (m > 2.5)
The last line on page 6 of the MPU9250 Datasheet:
states:
1MHz SPI serial interface for communicating with all registers
The next line:
20MHz SPI serial interface for reading sensor and interrupt registers
The FIFO register(Page 27 section 4.17 of the datasheet calls the FIFO a register) is not a sensor nor an interrupt register; therefore communications should be limited to 1 MHZ. Any scanning above this rate MAY(and will in the case of out of phase requests) give invalid/inconstant results.
I have not been able to find a reference for the MPU9250 library, or the commands given, to verify this is the case in your code.
I have below code two get a dot product of two vectors of size VECTORSIZE. Code works fine until VECTORSIZE up to 10000 but then it gives unrelated results. When I tried to debug the program I have seen that processor 0 (root) finishes its job before all processors send their local results. I got the same situation when I utilized the MPI_Reduce() (code part 2). However if I use MPI_Scatter() before MPI_Reduce() it is OK.
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
#define VECTORSIZE 10000000
#define ROOT 0
//[[## operation ConstructVectorPart()
void ConstructVector(double * vector, int size, short vectorEnu)
{
int i = 0;
if(vectorEnu == 1) // i.e vector 1
{
for(i = 0; i < size; i++)
{
vector[i] = 0.1 + (i%20)*0.1;
}
}
else if(vectorEnu == 2) // i.e. vector 2
{
for(i = 0 ; i < size; i++)
{
vector[i] = 2-(i%20)*0.1;
}
}
}
//[[## operation dotproduct()
double dotproduct(double* a, double* b, int length)
{
double result = 0;
int i = 0;
for (i = 0; i<length; i++)
result += a[i] * b[i];
return result;
}
int main( argc, argv )
int argc;
char **argv;
{
int processorID, numofProcessors;
int partialVectorSize ;
double t1, t2, localDotProduct, result;
MPI_Init( &argc, &argv );
MPI_Comm_size( MPI_COMM_WORLD, &numofProcessors );
MPI_Comm_rank( MPI_COMM_WORLD, &processorID );
if(processorID == 0)
t1 = MPI_Wtime();
// all processors constitute their own vector parts and
// calculates corresponding partial dot products
partialVectorSize = VECTORSIZE/ numofProcessors;
double *v1, *v2;
v1 = (double*)(malloc((partialVectorSize) * sizeof(double)));
v2 = (double*)(malloc((partialVectorSize) * sizeof(double)));
ConstructVectorPart(v1,0,partialVectorSize,1);
ConstructVectorPart(v2,0,partialVectorSize,2);
localDotProduct = dotproduct(v1,v2, partialVectorSize);
printf(" I am processor %d \n",processorID);
//----------------- code part 1 ---------------------------------------------
if( processorID != 0 ) // if not a master
{ // send partial result to master
MPI_Send( &localDotProduct, 1, MPI_DOUBLE, 0,0, MPI_COMM_WORLD );
}
else // master
{ // collect results
result = localDotProduct; // own result
int j;
for( j=1; j<numofProcessors; ++j )
{
MPI_Recv( &localDotProduct, 1, MPI_DOUBLE, j, 0, MPI_COMM_WORLD,MPI_STATUS_IGNORE);
result += localDotProduct;
}
t2 = MPI_Wtime();
printf(" result = %f TimeConsumed = %f \n",result, t2-t1);
}
//----------------------------------------------------------------------------
/*
//--------------------- code part 2 ----------------
MPI_Reduce(&localDotProduct, &result, 1, MPI_DOUBLE, MPI_SUM, 0,MPI_COMM_WORLD);
if(processorID == 0)
{
t2 = MPI_Wtime();
printf(" result = %f TimeConsumed = %f \n",result, t2-t1);
}
//---------------------------------------------------
*/
MPI_Finalize();
free(v1);
free(v2);
return 0;
}
On Unix, is there a command to display a file's modification time, precise to the second?
On Linux this is easily done with a "stat -c %y", which returns something like 2009-11-27 11:36:06.000000000 +0100. I found no equivalent on Unix.
I found this:
ls --time-style='+%d-%m-%Y %H:%M:%S' -l
Which exports something like this:
root:~# ls --time-style='+%d-%m-%Y %H:%M:%S' -l
total 0
-rw-r--r-- 1 root root 0 16-04-2015 23:14:02 other-file.txt
-rw-r--r-- 1 root root 0 16-04-2015 23:13:58 test.txt
According to the man page on my Mac (which has the BSD standard version of stat) you can get the epoch time version of the modification in seconds with:
stat -f %m /etc/passwd
Or if you want to print that out in hours:mins:secs you can do this:
perl -e "print scalar(localtime(`stat -f %m /etc/passwd`))"
The following gives you last modified time in seconds since Epoch:
stat -c%Y <file>
The find command is a good source for all kinds of file information, including modification time to the second:
find /etc/passwd -maxdepth 0 -printf "%TY/%Tm/%Td %TH:%TM:%.2TS\n"
2011/11/21 13:41:36
The first argument can be a file. The maxdepth prevents searching if a directory name is given. The %T instructs it to print last modification time.
Some systems interpret %TS as a floating point seconds (e.g. 36.8342610). If you want fractional seconds use "%TS" instead of "%.2TS", but you may not see fractional seconds on every system.
For anyone facing the same issue, I found no solution (on HP-UX 11i anyway).
Ended up coding a personalized "ls -lh" for my needs. It's not that hard..
Prints something like :
- 664 rw-/rw-/r-- 1L expertNoob adm 8.37 kB 2010.08.24 12:11:15 findf1.c
d 775 rwx/rwx/r-x 2L expertNoob adm 96 B 2010.08.24 15:17:37 tmp/
- 775 rwx/rwx/r-x 1L expertNoob adm 16 kB 2010.08.24 12:35:30 findf1
- 775 rwx/rwx/r-x 1L expertNoob adm 24 kB 2010.09.14 19:45:20 dir_info
- 444 r--/r--/r-- 1L expertNoob adm 9.01 kB 2010.09.01 11:23:41 getopt.c
- 664 rw-/rw-/r-- 1L expertNoob adm 6.86 kB 2010.09.01 11:24:47 getopt.o
- 664 rw-/rw-/r-- 1L expertNoob adm 6.93 kB 2010.09.14 19:37:44 findf1.o
l 775 rwx/rwx/r-x 1L expertNoob adm 6 B 2010.10.06 17:09:01 test1 -> test.c
- 664 rw-/rw-/r-- 1L expertNoob adm 534 B 2009.03.26 15:34:23 > test.c
d 755 rwx/r-x/r-x 25L expertNoob adm 8 kB 2009.05.20 15:36:23 zip30/
Here it is :
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include <langinfo.h>
#include <stdio.h>
//#include <stdint.h>
#include <limits.h> // PATH_MAX
#include <stdarg.h>
#include "getopt.h"
static short START_VSNBUFF=16;
// This is bformat from Better String library (bstrlib), customized
int strformat (char ** str, const char * fmt, ...) {
va_list arglist;
char * buff;
int n, r;
/* Since the length is not determinable beforehand, a search is
performed using the truncating "vsnprintf" call (to avoid buffer
overflows) on increasing potential sizes for the output result. */
if ((n = (int) (2*strlen (fmt))) < START_VSNBUFF) n = START_VSNBUFF;
if ( NULL == ( buff = (char *) malloc((n + 2)*sizeof(char)) ) ) {
n = 1;
if ( NULL == ( buff = (char *) malloc((n + 2)*sizeof(char)) ) ) {
fprintf( stderr, "strformat: not enough memory to format string\n" );
return -1;
}
}
for (;;) {
va_start (arglist, fmt);
r = vsnprintf (buff, n + 1, fmt, arglist); // n+1 chars: buff[0]..buff[n], n chars from arglist: buff[n]='\0'
va_end (arglist);
buff[n] = (unsigned char) '\0'; // doesn't hurt, especially strlen!
if ( strlen(buff) < n ) break;
if (r > n) n = r; else n += n;
if ( NULL == ( buff = (char *) realloc( buff, (n + 2)*sizeof(char) ) ) ) {
free(buff);
fprintf( stderr, "strformat: not enough memory to format string\n" );
return -1;
}
}
if( NULL != *str ) free(*str);
*str = buff;
return 0;
}
int printFSObjectInfo( const char * path, const char * name ) {
struct stat statbuf;
struct passwd *pwd;
struct group *grp;
struct tm *tm;
char datestring[256];
char *type = "? ";
char *fbuf = NULL;
double size = 0;
const char *units[] = {"B ", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
int i = 0;
char owner[] = "---", group[] = "---", others[] = "---";
/* Get entry's information. */
if ( -1 == lstat( path, &statbuf ) ) {
fprintf( stderr, "printFSObjectInfo: error: can't stat %s\n", path );
if( 0 == strformat( &fbuf, "lstat() said: %s", path ) ) { perror(fbuf); return -1; }
}
// File type
if( S_ISREG(statbuf.st_mode) ) type = "-"; // regular file
if( S_ISDIR(statbuf.st_mode) ) { // directory
type="d";
if( S_ISCDF(statbuf.st_mode) ) type = "hd"; // hidden dir
}
if( S_ISBLK(statbuf.st_mode) ) type = "b"; // block special
if( S_ISCHR(statbuf.st_mode) ) type = "c"; // character special
if( S_ISFIFO(statbuf.st_mode) ) type = "f"; // pipe or FIFO
if( S_ISLNK(statbuf.st_mode) ) type = "l"; // symbolic link
if( S_ISSOCK(statbuf.st_mode) ) type = "s"; // socket
if( S_ISNWK(statbuf.st_mode) ) type = "n"; // network special
printf( "%2s ", type );
/* Print out type, permissions, and number of links. */
//printf("%10.10s", sperm (statbuf.st_mode));
if( S_IRUSR & statbuf.st_mode ) owner[0] = 'r';
if( S_IWUSR & statbuf.st_mode ) owner[1] = 'w';
if( S_IXUSR & statbuf.st_mode ) owner[2] = 'x';
if( S_IRGRP & statbuf.st_mode ) group[0] = 'r';
if( S_IWGRP & statbuf.st_mode ) group[1] = 'w';
if( S_IXGRP & statbuf.st_mode ) group[2] = 'x';
if( S_IROTH & statbuf.st_mode ) others[0] = 'r';
if( S_IWOTH & statbuf.st_mode ) others[1] = 'w';
if( S_IXOTH & statbuf.st_mode ) others[2] = 'x';
//printf( "\n%o\n", statbuf.st_mode );
printf( "%3o %s/%s/%s ", 0777 & statbuf.st_mode, owner, group, others );
printf("%4dL", statbuf.st_nlink);
/* Print out owner's name if it is found using getpwuid(). */
if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
printf(" %-8.8s", pwd->pw_name);
else
printf(" %-8d", statbuf.st_uid);
/* Print out group name if it is found using getgrgid(). */
if ((grp = getgrgid(statbuf.st_gid)) != NULL)
printf(" %-8.8s", grp->gr_name);
else
printf(" %-8d", statbuf.st_gid);
/* Print size of file. */
//printf(" %9d", (int)statbuf.st_size);
i = 0;
size = (double) statbuf.st_size;
while (size >= 1024) {
size /= 1024;
i++;
}
if( 0 == (double)(size - (long) size) )
printf( "%7d %-2s", (long)size, units[i] );
else printf( "%7.2f %-2s", size, units[i] );
tm = localtime(&statbuf.st_mtime);
/* Get localized date string. */
strftime(datestring, sizeof(datestring), "%Y.%m.%d %T", tm); // nl_langinfo(D_T_FMT)
if ( 0 == strcmp(name, "\n") )
printf(" %s > %s", datestring, path);
else {
if( 0 == strcmp(type, "d") ) printf(" %s %s/", datestring, name);
else printf(" %s %s", datestring, name);
}
if( 0 == strcmp(type, "l") ) {
char buf[1+PATH_MAX];
if( -1 == readlink( path, buf, (1+PATH_MAX) ) ) {
fprintf( stderr, "printFSObjectInfo: error: can't read symbolic link %s\n", path);
if( 0 == strformat( &fbuf, "readlink() said: %s:", path ) ) { perror(fbuf); return -2; }
}
else {
lstat( buf, &statbuf ); // want errno, a symlink may point to non-existing object
if(errno == ENOENT) printf(" -> %s [!no such file!]\n", buf );
else {
printf(" -> %s\n", buf );
if ( 0 != strcmp(name, "\n") ) printFSObjectInfo( buf, "\n" );
}
}
}
else printf("\n");
return 0;
}
int main(int argc, char **argv) {
struct dirent *dp;
struct stat statbuf;
char *path = NULL; //[1+PATH_MAX];
char *fbuf = NULL;
char *pathArg = NULL;
if( argc == 1 || 0 == strlen(argv[1]) ) pathArg = ".";
else pathArg = argv[1];
if ( lstat( pathArg, &statbuf ) == -1 ) {
printf("%s: error: can't stat %s\n", argv[0], pathArg);
if( 0 == strformat( &fbuf, "stat() said: %s", pathArg ) ) perror(fbuf);
exit(2);
}
if( S_ISDIR(statbuf.st_mode) ) {
DIR *dir = opendir( pathArg );
if( NULL == dir ) {
fprintf( stderr, "%s: error: can't open %s\n", argv[0], pathArg );
if( 0 != strformat( &fbuf, "opendir() said: %s", pathArg ) ) exit(5);
perror(fbuf);
exit(4);
}
/* Loop through directory entries. */
while ( (dp = readdir(dir)) != NULL ) {
if( 0!= strformat( &path, "%s/%s", pathArg, dp->d_name ) ) continue;
printFSObjectInfo( path, dp->d_name );
}
closedir(dir);
} else printFSObjectInfo( pathArg, pathArg );
return 0;
}
In printFSObjectInfo() you have full functionality of lstat() system call, you can customize this to your wishes.
Be well.
Try a perl one-liner:
perl -e '#d=localtime ((stat(shift))[9]); printf "%02d-%02d-%04d %02d:%02d:%02d\n", $d[3],$d[4]+1,$d[5]+1900,$d[2],$d[1],$d[0]' your_file_to_show_the_date_for.your_extension
ls -le works if you need only HH:MM:SS
On Mac OS X (tested on 10.10.5 Yosemite thru 10.12.4 Sierra):
prompt> ls -lT
total 0
-rw-r--r-- 1 youruser staff 0 Sep 24 10:28:30 2015 my_file_1.txt
-rw-r--r-- 1 youruser staff 0 Sep 24 10:28:35 2015 my_file_2.txt
If you are using HP-UX:
Ok let's say that the name of the file is "junk". On HP-UX you can do:
perl -e '#d=localtime ((stat(shift))[9]); printf "%4d-%02d-%02d %02d:%02d:%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]' junk
And yes, perl comes with HP-UX. It is in /usr/contrib. But you may have a more recent version in /usr/local or /opt.
Source: Perderabo
Today I encountered the same issue on an old version of HP-UX. The stat program was not part of the installation. (just the C version)
The quickest solution for me was to use a tool such as Tectia file transfer running on my laptop, without actually doing any copying, It converts the time of last modification for you from HP-UX and provides dates and times for all files once you have logged into UNIX.
Possibly this works with other similar graphic based file transfer tools, but I have not tried yet.
On AIX the istat command does this:
machine:~/support> istat ../core
Inode 30034 on device 32/3 File
Protection: rw-rw-r--
Owner: 500(group) Group: 500(user)
Link count: 1 Length 10787748 bytes
Last updated: Wed Feb 22 13:54:28 2012
Last modified: Wed Feb 22 13:54:28 2012
Last accessed: Wed Feb 22 19:58:10 2012
Seconds:
date +%s -r /etc/passwd
Or, with more precision (up to nanosecond precision), if your filesystem supports it:
date +%s.%N -r /etc/passwd