问:
How do I profile C++ code running on Linux?
How do I find areas of my code that run slowly in a C++ application running on Linux?</p>

答:
Survey of C++ profiling techniques: gprof vs valgrind vs perf vs gperftools

In this answer, I will use several different tools to a analyze a few very simple test programs, in order to concretely compare how those tools work.

The following test program is very simple and does the following:

main calls fast and maybe_slow 3 times, one of the maybe_slow calls being slow

The slow call of maybe_slow is 10x longer, and dominates runtime if we consider calls to the child function common. Ideally, the profiling tool will be able to point us to the specific slow call.

both fast and maybe_slow call common, which accounts for the bulk of the program execution

The program interface is:

```<code>./main.out [n [seed]]
</code>```

and the program does O(n^2) loops in total. seed is just to get different output without affecting runtime.

main.c

```<code>#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>

uint64_t __attribute__ ((noinline)) common(uint64_t n, uint64_t seed) {
    for (uint64_t i = 0; i < n; ++i) {
        seed = (seed * seed) - (3 * seed) + 1;
    }
    return seed;
}

uint64_t __attribute__ ((noinline)) fast(uint64_t n, uint64_t seed) {
    uint64_t max = (n / 10) + 1;
    for (uint64_t i = 0; i < max; ++i) {
        seed = common(n, (seed * seed) - (3 * seed) + 1);
    }
    return seed;
}

uint64_t __attribute__ ((noinline)) maybe_slow(uint64_t n, uint64_t seed, int is_slow) {
    uint64_t max = n;
    if (is_slow) {
        max *= 10;
    }
    for (uint64_t i = 0; i < max; ++i) {
        seed = common(n, (seed * seed) - (3 * seed) + 1);
    }
    return seed;
}

int main(int argc, char **argv) {
    uint64_t n, seed;
    if (argc > 1) {
        n = strtoll(argv[1], NULL, 0);
    } else {
        n = 1;
    }
    if (argc > 2) {
        seed = strtoll(argv[2], NULL, 0);
    } else {
        seed = 0;
    }
    seed += maybe_slow(n, seed, 0);
    seed += fast(n, seed);
    seed += maybe_slow(n, seed, 1);
    seed += fast(n, seed);
    seed += maybe_slow(n, seed, 0);
    seed += fast(n, seed);
    printf("%" PRIX64 "\n", seed);
    return EXIT_SUCCESS;
}
</code>```

gprof

gprof requires recompiling the software with instrumentation, and it also uses a sampling approach together with that instrumentation. It therefore strikes a balance between accuracy (sampling is not always fully accurate and can skip functions) and execution slowdown (instrumentation and sampling are relatively fast techniques that don't slow down execution very much).

gprof is built-into GCC/binutils, so all we have to do is to compile with the -pg option to enable gprof. We then run the program normally with a size CLI parameter that produces a run of reasonable duration of a few seconds (10000):

```<code>gcc -pg -ggdb3 -O3 -std=c99 -Wall -Wextra -pedantic -o main.out main.c
time ./main.out 10000
</code>```

For educational reasons, we will also do a run without optimizations enabled. Note that this is useless in practice, as you normally only care about optimizing the performance of the optimized program:

```<code>gcc -pg -ggdb3 -O0 -std=c99 -Wall -Wextra -pedantic -o main.out main.c
./main.out 10000
</code>```

First, time tells us that the execution time with and without -pg were the same, which is great: no slowdown! I have however seen accounts of 2x - 3x slowdowns on complex software, e.g. as shown in this ticket.

Because we compiled with -pg, running the program produces a file gmon.out file containing the profiling data.

We can observe that file graphically with gprof2dot as asked at: Is it possible to get a graphical representation of gprof results?

```<code>sudo apt install graphviz
python3 -m pip install --user gprof2dot
gprof main.out > main.gprof
gprof2dot < main.gprof | dot -Tsvg -o output.svg
</code>```

Here, the gprof tool reads the gmon.out trace information, and generates a human readable report in main.gprof, which gprof2dot then reads to generate a graph.

The source for gprof2dot is at: https://github.com/jrfonseca/gprof2dot

We observe the following for the -O0 run:

and for the -O3 run:

The -O0 output is pretty much self-explanatory. For example, it shows that the 3 maybe_slow calls and their child calls take up 97.56% of the total runtime, although execution of maybe_slow itself without children accounts for 0.00% of the total execution time, i.e. almost all the time spent in that function was spent on child calls.

TODO: why is main missing from the -O3 output, even though I can see it on a bt in GDB? Missing function from GProf output I think it is because gprof is also sampling based in addition to its compiled instrumentation, and the -O3 main is just too fast and got no samples.

I choose SVG output instead of PNG because the SVG is searchable with Ctrl + F and the file size can be about 10x smaller. Also, the width and height of the generated image can be humoungous with tens of thousands of pixels for complex software, and GNOME eog 3.28.1 bugs out in that case for PNGs, while SVGs get opened by my browser automatically. gimp 2.8 worked well though, see also:

https://askubuntu.com/questions/1112641/how-to-view-extremely-large-images

https://unix.stackexchange.com/questions/77968/viewing-large-image-on-linux

https://superuser.com/questions/356038/viewer-for-huge-images-under-linux-100-mp-color-images

but even then, you will be dragging the image around a lot to find what you want, see e.g. this image from a "real" software example taken from this ticket:

Can you find the most critical call stack easily with all those tiny unsorted spaghetti lines going over one another? There might be better dot options I'm sure, but I don't want to go there now. What we really need is a proper dedicated viewer for it, but I haven't found one yet:

View gprof output in kcachegrind

Which is the best replacement for KProf?

You can however use the color map to mitigate those problems a bit. For example, on the previous huge image, I finally managed to find the critical path on the left when I made the brilliant deduction that green comes after red, followed finally by darker and darker blue.

Alternatively, we can also observe the text output of the gprof built-in binutils tool which we previously saved at:

```<code>cat main.gprof
</code>```

By default, this produces an extremely verbose output that explains what the output data means. Since I can't explain better than that, I'll let you read it yourself.

Once you have understood the data output format, you can reduce verbosity to show just the data without the tutorial with the -b option:

```<code>gprof -b main.out
</code>```

In our example, outputs were for -O0:

```<code>Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls   s/call   s/call  name
100.35      3.67     3.67   123003     0.00     0.00  common
  0.00      3.67     0.00        3     0.00     0.03  fast
  0.00      3.67     0.00        3     0.00     1.19  maybe_slow

            Call graph

granularity: each sample hit covers 2 byte(s) for 0.27% of 3.67 seconds

index % time    self  children    called     name
                0.09    0.00    3003/123003      fast [4]
                3.58    0.00  120000/123003      maybe_slow [3]
[1]    100.0    3.67    0.00  123003         common [1]
-----------------------------------------------
                                                 <spontaneous>
[2]    100.0    0.00    3.67                 main [2]
                0.00    3.58       3/3           maybe_slow [3]
                0.00    0.09       3/3           fast [4]
-----------------------------------------------
                0.00    3.58       3/3           main [2]
[3]     97.6    0.00    3.58       3         maybe_slow [3]
                3.58    0.00  120000/123003      common [1]
-----------------------------------------------
                0.00    0.09       3/3           main [2]
[4]      2.4    0.00    0.09       3         fast [4]
                0.09    0.00    3003/123003      common [1]
-----------------------------------------------

Index by function name

   [1] common                  [4] fast                    [3] maybe_slow
</code>```

and for -O3:

```<code>Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  us/call  us/call  name
100.52      1.84     1.84   123003    14.96    14.96  common

            Call graph

granularity: each sample hit covers 2 byte(s) for 0.54% of 1.84 seconds

index % time    self  children    called     name
                0.04    0.00    3003/123003      fast [3]
                1.79    0.00  120000/123003      maybe_slow [2]
[1]    100.0    1.84    0.00  123003         common [1]
-----------------------------------------------
                                                 <spontaneous>
[2]     97.6    0.00    1.79                 maybe_slow [2]
                1.79    0.00  120000/123003      common [1]
-----------------------------------------------
                                                 <spontaneous>
[3]      2.4    0.00    0.04                 fast [3]
                0.04    0.00    3003/123003      common [1]
-----------------------------------------------

Index by function name

   [1] common
</code>```

As a very quick summary for each section e.g.:

```<code>                0.00    3.58       3/3           main [2]
[3]     97.6    0.00    3.58       3         maybe_slow [3]
                3.58    0.00  120000/123003      common [1]
</code>```

centers around the function that is left indented (maybe_flow). [3] is the ID of that function. Above the function, are its callers, and below it the callees.

For -O3, see here like in the graphical output that maybe_slow and fast don't have a known parent, which is what the documentation says that <spontaneous> means.

I'm not sure if there is a nice way to do line-by-line profiling with gprof: `gprof` time spent in particular lines of code

valgrind callgrind

valgrind runs the program through the valgrind virtual machine. This makes the profiling very accurate, but it also produces a very large slowdown of the program. I have also mentioned kcachegrind previously at: Tools to get a pictorial function call graph of code

callgrind is the valgrind's tool to profile code and kcachegrind is a KDE program that can visualize cachegrind output.

First we have to remove the -pg flag to go back to normal compilation, otherwise the run actually fails with Profiling timer expired, and yes, this is so common that I did and there was a Stack Overflow question for it.

So we compile and run as:

```<code>#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>

uint64_t __attribute__ ((noinline)) common(uint64_t n, uint64_t seed) {
    for (uint64_t i = 0; i < n; ++i) {
        seed = (seed * seed) - (3 * seed) + 1;
    }
    return seed;
}

uint64_t __attribute__ ((noinline)) fast(uint64_t n, uint64_t seed) {
    uint64_t max = (n / 10) + 1;
    for (uint64_t i = 0; i < max; ++i) {
        seed = common(n, (seed * seed) - (3 * seed) + 1);
    }
    return seed;
}

uint64_t __attribute__ ((noinline)) maybe_slow(uint64_t n, uint64_t seed, int is_slow) {
    uint64_t max = n;
    if (is_slow) {
        max *= 10;
    }
    for (uint64_t i = 0; i < max; ++i) {
        seed = common(n, (seed * seed) - (3 * seed) + 1);
    }
    return seed;
}

int main(int argc, char **argv) {
    uint64_t n, seed;
    if (argc > 1) {
        n = strtoll(argv[1], NULL, 0);
    } else {
        n = 1;
    }
    if (argc > 2) {
        seed = strtoll(argv[2], NULL, 0);
    } else {
        seed = 0;
    }
    seed += maybe_slow(n, seed, 0);
    seed += fast(n, seed);
    seed += maybe_slow(n, seed, 1);
    seed += fast(n, seed);
    seed += maybe_slow(n, seed, 0);
    seed += fast(n, seed);
    printf("%" PRIX64 "\n", seed);
    return EXIT_SUCCESS;
}
</code>```0

I enable --dump-instr=yes --collect-jumps=yes because this also dumps information that enables us to view a per assembly line breakdown of performance, at a relatively small added overhead cost.

Off the bat, time tells us that the program took 29.5 seconds to execute, so we had a slowdown of about 15x on this example. Clearly, this slowdown is going to be a serious limitation for larger workloads. On the "real world software example" mentioned here, I observed a slowdown of 80x.

The run generates a profile data file named callgrind.out.<pid> e.g. callgrind.out.8554 in my case. We view that file with:

```<code>#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>

uint64_t __attribute__ ((noinline)) common(uint64_t n, uint64_t seed) {
    for (uint64_t i = 0; i < n; ++i) {
        seed = (seed * seed) - (3 * seed) + 1;
    }
    return seed;
}

uint64_t __attribute__ ((noinline)) fast(uint64_t n, uint64_t seed) {
    uint64_t max = (n / 10) + 1;
    for (uint64_t i = 0; i < max; ++i) {
        seed = common(n, (seed * seed) - (3 * seed) + 1);
    }
    return seed;
}

uint64_t __attribute__ ((noinline)) maybe_slow(uint64_t n, uint64_t seed, int is_slow) {
    uint64_t max = n;
    if (is_slow) {
        max *= 10;
    }
    for (uint64_t i = 0; i < max; ++i) {
        seed = common(n, (seed * seed) - (3 * seed) + 1);
    }
    return seed;
}

int main(int argc, char **argv) {
    uint64_t n, seed;
    if (argc > 1) {
        n = strtoll(argv[1], NULL, 0);
    } else {
        n = 1;
    }
    if (argc > 2) {
        seed = strtoll(argv[2], NULL, 0);
    } else {
        seed = 0;
    }
    seed += maybe_slow(n, seed, 0);
    seed += fast(n, seed);
    seed += maybe_slow(n, seed, 1);
    seed += fast(n, seed);
    seed += maybe_slow(n, seed, 0);
    seed += fast(n, seed);
    printf("%" PRIX64 "\n", seed);
    return EXIT_SUCCESS;
}
</code>```1

which shows a GUI that contains data similar to the textual gprof output:

Also, if we go on the bottom right "Call Graph" tab, we see a call graph which we can export by right clicking it to obtain the following image with unreasonable amounts of white border :-)

I think fast is not showing on that graph because kcachegrind must have simplified the visualization because that call takes up too little time, this will likely be the behavior you want on a real program. The right click menu has some settings to control when to cull such nodes, but I couldn't get it to show such a short call after a quick attempt. If I click on fast on the left window, it does show a call graph with fast, so that stack was actually captured. No one had yet found a way to show the complete graph call graph: Make callgrind show all function calls in the kcachegrind callgraph

TODO on complex C++ software, I see some entries of type <cycle N>, e.g. <cycle 11> where I'd expect function names, what does that mean? I noticed there is a "Cycle Detection" button to toggle that on and off, but what does it mean?

perf from linux-tools

perf seems to use exclusively Linux kernel sampling mechanisms. This makes it very simple to setup, but also not fully accurate.

```<code>#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>

uint64_t __attribute__ ((noinline)) common(uint64_t n, uint64_t seed) {
    for (uint64_t i = 0; i < n; ++i) {
        seed = (seed * seed) - (3 * seed) + 1;
    }
    return seed;
}

uint64_t __attribute__ ((noinline)) fast(uint64_t n, uint64_t seed) {
    uint64_t max = (n / 10) + 1;
    for (uint64_t i = 0; i < max; ++i) {
        seed = common(n, (seed * seed) - (3 * seed) + 1);
    }
    return seed;
}

uint64_t __attribute__ ((noinline)) maybe_slow(uint64_t n, uint64_t seed, int is_slow) {
    uint64_t max = n;
    if (is_slow) {
        max *= 10;
    }
    for (uint64_t i = 0; i < max; ++i) {
        seed = common(n, (seed * seed) - (3 * seed) + 1);
    }
    return seed;
}

int main(int argc, char **argv) {
    uint64_t n, seed;
    if (argc > 1) {
        n = strtoll(argv[1], NULL, 0);
    } else {
        n = 1;
    }
    if (argc > 2) {
        seed = strtoll(argv[2], NULL, 0);
    } else {
        seed = 0;
    }
    seed += maybe_slow(n, seed, 0);
    seed += fast(n, seed);
    seed += maybe_slow(n, seed, 1);
    seed += fast(n, seed);
    seed += maybe_slow(n, seed, 0);
    seed += fast(n, seed);
    printf("%" PRIX64 "\n", seed);
    return EXIT_SUCCESS;
}
</code>```2

This added 0.2s to execution, so we are fine time-wise, but I still don't see much of interest, after expanding the common node with the keyboard right arrow:

```<code>Samples: 7K of event 'cycles:uppp', Event count (approx.): 6228527608
  Children      Self  Command   Shared Object     Symbol
-   99.98%    99.88%  main.out  main.out          [.] common
     common
     0.11%     0.11%  main.out  [kernel]          [k] 0xffffffff8a6009e7
     0.01%     0.01%  main.out  [kernel]          [k] 0xffffffff8a600158
     0.01%     0.00%  main.out  [unknown]         [k] 0x0000000000000040
     0.01%     0.00%  main.out  ld-2.27.so        [.] _dl_sysdep_start
     0.01%     0.00%  main.out  ld-2.27.so        [.] dl_main
     0.01%     0.00%  main.out  ld-2.27.so        [.] mprotect
     0.01%     0.00%  main.out  ld-2.27.so        [.] _dl_map_object
     0.01%     0.00%  main.out  ld-2.27.so        [.] _xstat
     0.00%     0.00%  main.out  ld-2.27.so        [.] __GI___tunables_init
     0.00%     0.00%  main.out  [unknown]         [.] 0x2f3d4f4944555453
     0.00%     0.00%  main.out  [unknown]         [.] 0x00007fff3cfc57ac
     0.00%     0.00%  main.out  ld-2.27.so        [.] _start
</code>```

So then I try to benchmark the -O0 program to see if that shows anything, and only now, at last, do I see a call graph:

```<code>Samples: 15K of event 'cycles:uppp', Event count (approx.): 12438962281
  Children      Self  Command   Shared Object     Symbol
+   99.99%     0.00%  main.out  [unknown]         [.] 0x04be258d4c544155
+   99.99%     0.00%  main.out  libc-2.27.so      [.] __libc_start_main
-   99.99%     0.00%  main.out  main.out          [.] main
   - main
      - 97.54% maybe_slow
           common
      - 2.45% fast
           common
+   99.96%    99.85%  main.out  main.out          [.] common
+   97.54%     0.03%  main.out  main.out          [.] maybe_slow
+    2.45%     0.00%  main.out  main.out          [.] fast
     0.11%     0.11%  main.out  [kernel]          [k] 0xffffffff8a6009e7
     0.00%     0.00%  main.out  [unknown]         [k] 0x0000000000000040
     0.00%     0.00%  main.out  ld-2.27.so        [.] _dl_sysdep_start
     0.00%     0.00%  main.out  ld-2.27.so        [.] dl_main
     0.00%     0.00%  main.out  ld-2.27.so        [.] _dl_lookup_symbol_x
     0.00%     0.00%  main.out  [kernel]          [k] 0xffffffff8a600158
     0.00%     0.00%  main.out  ld-2.27.so        [.] mmap64
     0.00%     0.00%  main.out  ld-2.27.so        [.] _dl_map_object
     0.00%     0.00%  main.out  ld-2.27.so        [.] __GI___tunables_init
     0.00%     0.00%  main.out  [unknown]         [.] 0x552e53555f6e653d
     0.00%     0.00%  main.out  [unknown]         [.] 0x00007ffe1cf20fdb
     0.00%     0.00%  main.out  ld-2.27.so        [.] _start
</code>```

TODO: what happened on the -O3 execution? Is it simply that maybe_slow and fast were too fast and did not get any samples? Does it work well with -O3 on larger programs that take longer to execute? Did I miss some CLI option? I found out about -F to control the sample frequency in Hertz, but I turned it up to the max allowed by default of -F 39500 (could be increased with sudo) and I still don't see clear calls.

One cool thing about perf is the FlameGraph tool from Brendan Gregg which displays the call stack timings in a very neat way that allows you to quickly see the big calls. The tool is available at: https://github.com/brendangregg/FlameGraph and is also mentioned on his perf tutorial at: http://www.brendangregg.com/perf.html#FlameGraphs When I ran perf without sudo I got ERROR: No stack counts found so for now I'll be doing it with sudo:

```<code>git clone https://github.com/brendangregg/FlameGraph
sudo perf record -F 99 -g -o perf_with_stack.data ./main.out 10000
sudo perf script -i perf_with_stack.data | FlameGraph/stackcollapse-perf.pl | FlameGraph/flamegraph.pl > flamegraph.svg
</code>```

but in such a simple program the output is not very easy to understand, since we can't easily see neither maybe_slow nor fast on that graph:

On the a more complex example it becomes clear what the graph means:

TODO there are a log of [unknown] functions in that example, why is that?

Another perf GUI interfaces which might be worth it include:

Eclipse Trace Compass plugin: https://www.eclipse.org/tracecompass/

But this has the downside that you have to first convert the data to the Common Trace Format, which can be done with perf data --to-ctf, but it needs to be enabled at build time/have perf new enough, either of which is not the case for the perf in Ubuntu 18.04

https://github.com/KDAB/hotspot

The downside of this is that there seems to be no Ubuntu package, and building it requires Qt 5.10 while Ubuntu 18.04 is at Qt 5.9.

But David Faure mentions in the comments that there is no an AppImage package which might be a convenient way to use it.

gperftools

Previously called "Google Performance Tools", source: https://github.com/gperftools/gperftools Sample based.

First install gperftools with:

```<code>sudo apt install google-perftools
</code>```

Then, we can enable the gperftools CPU profiler in two ways: at runtime, or at build time.

At runtime, we have to pass set the LD_PRELOAD to point to libprofiler.so, which you can find with locate libprofiler.so, e.g. on my system:

```<code>gcc -ggdb3 -O3 -std=c99 -Wall -Wextra -pedantic -o main.out main.c
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libprofiler.so \
  CPUPROFILE=prof.out ./main.out 10000
</code>```

Alternatively, we can build the library in at link time, dispensing passing LD_PRELOAD at runtime:

```<code>gcc -Wl,--no-as-needed,-lprofiler,--as-needed -ggdb3 -O3 -std=c99 -Wall -Wextra -pedantic -o main.out main.c
CPUPROFILE=prof.out ./main.out 10000
</code>```

See also: gperftools - profile file not dumped

The nicest way to view this data I've found so far is to make pprof output the same format that kcachegrind takes as input (yes, the Valgrind-project-viewer-tool) and use kcachegrind to view that:

```<code>google-pprof --callgrind main.out prof.out  > callgrind.out
kcachegrind callgrind.out
</code>```

After running with either of those methods, we get a prof.out profile data file as output. We can view that file graphically as an SVG with:

```<code>gcc -pg -ggdb3 -O3 -std=c99 -Wall -Wextra -pedantic -o main.out main.c
time ./main.out 10000
</code>```0

which gives as a familiar call graph like other tools, but with the clunky unit of number of samples rather than seconds.

Alternatively, we can also get some textual data with:

```<code>gcc -pg -ggdb3 -O3 -std=c99 -Wall -Wextra -pedantic -o main.out main.c
time ./main.out 10000
</code>```1

which gives:

```<code>gcc -pg -ggdb3 -O3 -std=c99 -Wall -Wextra -pedantic -o main.out main.c
time ./main.out 10000
</code>```2

See also: How to use google perf tools

Instrument your code with raw perf_event_open syscalls

I think this is the same underlying subsystem that perf uses, but you could of course attain even greater control by explicitly instrumenting your program at compile time with events of interest.

This is likely too hardcore for most people, but it's kind of fun. Minimal runnable example at: Quick way to count number of instructions executed in a C program

Intel VTune

https://en.wikipedia.org/wiki/VTune

This seems to be closed source and x86-only, but it is likely to be amazing from what I've heard. I'm not sure how free it is to use, but it seems to be free to download. TODO evaluate.

Tested in Ubuntu 18.04, gprof2dot 2019.11.30, valgrind 3.13.0, perf 4.15.18, Linux kernel 4.15.0, FLameGraph 1a0dc6985aad06e76857cf2a354bd5ba0c9ce96b, gperftools 2.5-2.

OwnerDisplayName: Gabriel Isenberg

Body:
Survey of C++ profiling techniques: gprof vs valgrind vs perf vs gperftools

In this answer, I will use several different tools to a analyze a few very simple test programs, in order to concretely compare how those tools work.

The following test program is very simple and does the following:

main calls fast and maybe_slow 3 times, one of the maybe_slow calls being slow

The slow call of maybe_slow is 10x longer, and dominates runtime if we consider calls to the child function common. Ideally, the profiling tool will be able to point us to the specific slow call.

both fast and maybe_slow call common, which accounts for the bulk of the program execution

The program interface is:

./main.out [n [seed]]

and the program does O(n^2) loops in total. seed is just to get different output without affecting runtime.

main.c

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>

uint64_t __attribute__ ((noinline)) common(uint64_t n, uint64_t seed) {
    for (uint64_t i = 0; i < n; ++i) {
        seed = (seed * seed) - (3 * seed) + 1;
    }
    return seed;
}

uint64_t __attribute__ ((noinline)) fast(uint64_t n, uint64_t seed) {
    uint64_t max = (n / 10) + 1;
    for (uint64_t i = 0; i < max; ++i) {
        seed = common(n, (seed * seed) - (3 * seed) + 1);
    }
    return seed;
}

uint64_t __attribute__ ((noinline)) maybe_slow(uint64_t n, uint64_t seed, int is_slow) {
    uint64_t max = n;
    if (is_slow) {
        max *= 10;
    }
    for (uint64_t i = 0; i < max; ++i) {
        seed = common(n, (seed * seed) - (3 * seed) + 1);
    }
    return seed;
}

int main(int argc, char **argv) {
    uint64_t n, seed;
    if (argc > 1) {
        n = strtoll(argv[1], NULL, 0);
    } else {
        n = 1;
    }
    if (argc > 2) {
        seed = strtoll(argv[2], NULL, 0);
    } else {
        seed = 0;
    }
    seed += maybe_slow(n, seed, 0);
    seed += fast(n, seed);
    seed += maybe_slow(n, seed, 1);
    seed += fast(n, seed);
    seed += maybe_slow(n, seed, 0);
    seed += fast(n, seed);
    printf("%" PRIX64 "\n", seed);
    return EXIT_SUCCESS;
}

gprof

gprof requires recompiling the software with instrumentation, and it also uses a sampling approach together with that instrumentation. It therefore strikes a balance between accuracy (sampling is not always fully accurate and can skip functions) and execution slowdown (instrumentation and sampling are relatively fast techniques that don't slow down execution very much).

gprof is built-into GCC/binutils, so all we have to do is to compile with the -pg option to enable gprof. We then run the program normally with a size CLI parameter that produces a run of reasonable duration of a few seconds (10000):

gcc -pg -ggdb3 -O3 -std=c99 -Wall -Wextra -pedantic -o main.out main.c
time ./main.out 10000

For educational reasons, we will also do a run without optimizations enabled. Note that this is useless in practice, as you normally only care about optimizing the performance of the optimized program:

gcc -pg -ggdb3 -O0 -std=c99 -Wall -Wextra -pedantic -o main.out main.c
./main.out 10000

First, time tells us that the execution time with and without -pg were the same, which is great: no slowdown! I have however seen accounts of 2x - 3x slowdowns on complex software, e.g. as shown in this ticket.

Because we compiled with -pg, running the program produces a file gmon.out file containing the profiling data.

We can observe that file graphically with gprof2dot as asked at: Is it possible to get a graphical representation of gprof results?

sudo apt install graphviz
python3 -m pip install --user gprof2dot
gprof main.out > main.gprof
gprof2dot < main.gprof | dot -Tsvg -o output.svg

Here, the gprof tool reads the gmon.out trace information, and generates a human readable report in main.gprof, which gprof2dot then reads to generate a graph.

The source for gprof2dot is at: https://github.com/jrfonseca/gprof2dot

We observe the following for the -O0 run:

and for the -O3 run:

The -O0 output is pretty much self-explanatory. For example, it shows that the 3 maybe_slow calls and their child calls take up 97.56% of the total runtime, although execution of maybe_slow itself without children accounts for 0.00% of the total execution time, i.e. almost all the time spent in that function was spent on child calls.

TODO: why is main missing from the -O3 output, even though I can see it on a bt in GDB? Missing function from GProf output I think it is because gprof is also sampling based in addition to its compiled instrumentation, and the -O3 main is just too fast and got no samples.

I choose SVG output instead of PNG because the SVG is searchable with Ctrl + F and the file size can be about 10x smaller. Also, the width and height of the generated image can be humoungous with tens of thousands of pixels for complex software, and GNOME eog 3.28.1 bugs out in that case for PNGs, while SVGs get opened by my browser automatically. gimp 2.8 worked well though, see also:

https://askubuntu.com/questions/1112641/how-to-view-extremely-large-images

https://unix.stackexchange.com/questions/77968/viewing-large-image-on-linux

https://superuser.com/questions/356038/viewer-for-huge-images-under-linux-100-mp-color-images

but even then, you will be dragging the image around a lot to find what you want, see e.g. this image from a "real" software example taken from this ticket:

Can you find the most critical call stack easily with all those tiny unsorted spaghetti lines going over one another? There might be better dot options I'm sure, but I don't want to go there now. What we really need is a proper dedicated viewer for it, but I haven't found one yet:

View gprof output in kcachegrind

Which is the best replacement for KProf?

You can however use the color map to mitigate those problems a bit. For example, on the previous huge image, I finally managed to find the critical path on the left when I made the brilliant deduction that green comes after red, followed finally by darker and darker blue.

Alternatively, we can also observe the text output of the gprof built-in binutils tool which we previously saved at:

cat main.gprof

By default, this produces an extremely verbose output that explains what the output data means. Since I can't explain better than that, I'll let you read it yourself.

Once you have understood the data output format, you can reduce verbosity to show just the data without the tutorial with the -b option:

gprof -b main.out

In our example, outputs were for -O0:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls   s/call   s/call  name
100.35      3.67     3.67   123003     0.00     0.00  common
  0.00      3.67     0.00        3     0.00     0.03  fast
  0.00      3.67     0.00        3     0.00     1.19  maybe_slow

            Call graph

granularity: each sample hit covers 2 byte(s) for 0.27% of 3.67 seconds

index % time    self  children    called     name
                0.09    0.00    3003/123003      fast [4]
                3.58    0.00  120000/123003      maybe_slow [3]
[1]    100.0    3.67    0.00  123003         common [1]
-----------------------------------------------
                                                 <spontaneous>
[2]    100.0    0.00    3.67                 main [2]
                0.00    3.58       3/3           maybe_slow [3]
                0.00    0.09       3/3           fast [4]
-----------------------------------------------
                0.00    3.58       3/3           main [2]
[3]     97.6    0.00    3.58       3         maybe_slow [3]
                3.58    0.00  120000/123003      common [1]
-----------------------------------------------
                0.00    0.09       3/3           main [2]
[4]      2.4    0.00    0.09       3         fast [4]
                0.09    0.00    3003/123003      common [1]
-----------------------------------------------

Index by function name

   [1] common                  [4] fast                    [3] maybe_slow

and for -O3:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  us/call  us/call  name
100.52      1.84     1.84   123003    14.96    14.96  common

            Call graph

granularity: each sample hit covers 2 byte(s) for 0.54% of 1.84 seconds

index % time    self  children    called     name
                0.04    0.00    3003/123003      fast [3]
                1.79    0.00  120000/123003      maybe_slow [2]
[1]    100.0    1.84    0.00  123003         common [1]
-----------------------------------------------
                                                 <spontaneous>
[2]     97.6    0.00    1.79                 maybe_slow [2]
                1.79    0.00  120000/123003      common [1]
-----------------------------------------------
                                                 <spontaneous>
[3]      2.4    0.00    0.04                 fast [3]
                0.04    0.00    3003/123003      common [1]
-----------------------------------------------

Index by function name

   [1] common

As a very quick summary for each section e.g.:

                0.00    3.58       3/3           main [2]
[3]     97.6    0.00    3.58       3         maybe_slow [3]
                3.58    0.00  120000/123003      common [1]

centers around the function that is left indented (maybe_flow). [3] is the ID of that function. Above the function, are its callers, and below it the callees.

For -O3, see here like in the graphical output that maybe_slow and fast don't have a known parent, which is what the documentation says that <spontaneous> means.

I'm not sure if there is a nice way to do line-by-line profiling with gprof: `gprof` time spent in particular lines of code

valgrind callgrind

valgrind runs the program through the valgrind virtual machine. This makes the profiling very accurate, but it also produces a very large slowdown of the program. I have also mentioned kcachegrind previously at: Tools to get a pictorial function call graph of code

callgrind is the valgrind's tool to profile code and kcachegrind is a KDE program that can visualize cachegrind output.

First we have to remove the -pg flag to go back to normal compilation, otherwise the run actually fails with Profiling timer expired, and yes, this is so common that I did and there was a Stack Overflow question for it.

So we compile and run as:

sudo apt install kcachegrind valgrind
gcc -ggdb3 -O3 -std=c99 -Wall -Wextra -pedantic -o main.out main.c
time valgrind --tool=callgrind valgrind --dump-instr=yes \
  --collect-jumps=yes ./main.out 10000

I enable --dump-instr=yes --collect-jumps=yes because this also dumps information that enables us to view a per assembly line breakdown of performance, at a relatively small added overhead cost.

Off the bat, time tells us that the program took 29.5 seconds to execute, so we had a slowdown of about 15x on this example. Clearly, this slowdown is going to be a serious limitation for larger workloads. On the "real world software example" mentioned here, I observed a slowdown of 80x.

The run generates a profile data file named callgrind.out.<pid> e.g. callgrind.out.8554 in my case. We view that file with:

kcachegrind callgrind.out.8554

which shows a GUI that contains data similar to the textual gprof output:

Also, if we go on the bottom right "Call Graph" tab, we see a call graph which we can export by right clicking it to obtain the following image with unreasonable amounts of white border :-)

I think fast is not showing on that graph because kcachegrind must have simplified the visualization because that call takes up too little time, this will likely be the behavior you want on a real program. The right click menu has some settings to control when to cull such nodes, but I couldn't get it to show such a short call after a quick attempt. If I click on fast on the left window, it does show a call graph with fast, so that stack was actually captured. No one had yet found a way to show the complete graph call graph: Make callgrind show all function calls in the kcachegrind callgraph

TODO on complex C++ software, I see some entries of type <cycle N>, e.g. <cycle 11> where I'd expect function names, what does that mean? I noticed there is a "Cycle Detection" button to toggle that on and off, but what does it mean?

perf from linux-tools

perf seems to use exclusively Linux kernel sampling mechanisms. This makes it very simple to setup, but also not fully accurate.

sudo apt install linux-tools
time perf record -g ./main.out 10000

This added 0.2s to execution, so we are fine time-wise, but I still don't see much of interest, after expanding the common node with the keyboard right arrow:

Samples: 7K of event 'cycles:uppp', Event count (approx.): 6228527608
  Children      Self  Command   Shared Object     Symbol
-   99.98%    99.88%  main.out  main.out          [.] common
     common
     0.11%     0.11%  main.out  [kernel]          [k] 0xffffffff8a6009e7
     0.01%     0.01%  main.out  [kernel]          [k] 0xffffffff8a600158
     0.01%     0.00%  main.out  [unknown]         [k] 0x0000000000000040
     0.01%     0.00%  main.out  ld-2.27.so        [.] _dl_sysdep_start
     0.01%     0.00%  main.out  ld-2.27.so        [.] dl_main
     0.01%     0.00%  main.out  ld-2.27.so        [.] mprotect
     0.01%     0.00%  main.out  ld-2.27.so        [.] _dl_map_object
     0.01%     0.00%  main.out  ld-2.27.so        [.] _xstat
     0.00%     0.00%  main.out  ld-2.27.so        [.] __GI___tunables_init
     0.00%     0.00%  main.out  [unknown]         [.] 0x2f3d4f4944555453
     0.00%     0.00%  main.out  [unknown]         [.] 0x00007fff3cfc57ac
     0.00%     0.00%  main.out  ld-2.27.so        [.] _start

So then I try to benchmark the -O0 program to see if that shows anything, and only now, at last, do I see a call graph:

Samples: 15K of event 'cycles:uppp', Event count (approx.): 12438962281
  Children      Self  Command   Shared Object     Symbol
+   99.99%     0.00%  main.out  [unknown]         [.] 0x04be258d4c544155
+   99.99%     0.00%  main.out  libc-2.27.so      [.] __libc_start_main
-   99.99%     0.00%  main.out  main.out          [.] main
   - main
      - 97.54% maybe_slow
           common
      - 2.45% fast
           common
+   99.96%    99.85%  main.out  main.out          [.] common
+   97.54%     0.03%  main.out  main.out          [.] maybe_slow
+    2.45%     0.00%  main.out  main.out          [.] fast
     0.11%     0.11%  main.out  [kernel]          [k] 0xffffffff8a6009e7
     0.00%     0.00%  main.out  [unknown]         [k] 0x0000000000000040
     0.00%     0.00%  main.out  ld-2.27.so        [.] _dl_sysdep_start
     0.00%     0.00%  main.out  ld-2.27.so        [.] dl_main
     0.00%     0.00%  main.out  ld-2.27.so        [.] _dl_lookup_symbol_x
     0.00%     0.00%  main.out  [kernel]          [k] 0xffffffff8a600158
     0.00%     0.00%  main.out  ld-2.27.so        [.] mmap64
     0.00%     0.00%  main.out  ld-2.27.so        [.] _dl_map_object
     0.00%     0.00%  main.out  ld-2.27.so        [.] __GI___tunables_init
     0.00%     0.00%  main.out  [unknown]         [.] 0x552e53555f6e653d
     0.00%     0.00%  main.out  [unknown]         [.] 0x00007ffe1cf20fdb
     0.00%     0.00%  main.out  ld-2.27.so        [.] _start

TODO: what happened on the -O3 execution? Is it simply that maybe_slow and fast were too fast and did not get any samples? Does it work well with -O3 on larger programs that take longer to execute? Did I miss some CLI option? I found out about -F to control the sample frequency in Hertz, but I turned it up to the max allowed by default of -F 39500 (could be increased with sudo) and I still don't see clear calls.

One cool thing about perf is the FlameGraph tool from Brendan Gregg which displays the call stack timings in a very neat way that allows you to quickly see the big calls. The tool is available at: https://github.com/brendangregg/FlameGraph and is also mentioned on his perf tutorial at: http://www.brendangregg.com/perf.html#FlameGraphs When I ran perf without sudo I got ERROR: No stack counts found so for now I'll be doing it with sudo:

git clone https://github.com/brendangregg/FlameGraph
sudo perf record -F 99 -g -o perf_with_stack.data ./main.out 10000
sudo perf script -i perf_with_stack.data | FlameGraph/stackcollapse-perf.pl | FlameGraph/flamegraph.pl > flamegraph.svg

but in such a simple program the output is not very easy to understand, since we can't easily see neither maybe_slow nor fast on that graph:

On the a more complex example it becomes clear what the graph means:

TODO there are a log of [unknown] functions in that example, why is that?

Another perf GUI interfaces which might be worth it include:

Eclipse Trace Compass plugin: https://www.eclipse.org/tracecompass/

But this has the downside that you have to first convert the data to the Common Trace Format, which can be done with perf data --to-ctf, but it needs to be enabled at build time/have perf new enough, either of which is not the case for the perf in Ubuntu 18.04

https://github.com/KDAB/hotspot

The downside of this is that there seems to be no Ubuntu package, and building it requires Qt 5.10 while Ubuntu 18.04 is at Qt 5.9.

But David Faure mentions in the comments that there is no an AppImage package which might be a convenient way to use it.

gperftools

Previously called "Google Performance Tools", source: https://github.com/gperftools/gperftools Sample based.

First install gperftools with:

sudo apt install google-perftools

Then, we can enable the gperftools CPU profiler in two ways: at runtime, or at build time.

At runtime, we have to pass set the LD_PRELOAD to point to libprofiler.so, which you can find with locate libprofiler.so, e.g. on my system:

gcc -ggdb3 -O3 -std=c99 -Wall -Wextra -pedantic -o main.out main.c
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libprofiler.so \
  CPUPROFILE=prof.out ./main.out 10000

Alternatively, we can build the library in at link time, dispensing passing LD_PRELOAD at runtime:

gcc -Wl,--no-as-needed,-lprofiler,--as-needed -ggdb3 -O3 -std=c99 -Wall -Wextra -pedantic -o main.out main.c
CPUPROFILE=prof.out ./main.out 10000

See also: gperftools - profile file not dumped

The nicest way to view this data I've found so far is to make pprof output the same format that kcachegrind takes as input (yes, the Valgrind-project-viewer-tool) and use kcachegrind to view that:

google-pprof --callgrind main.out prof.out  > callgrind.out
kcachegrind callgrind.out

After running with either of those methods, we get a prof.out profile data file as output. We can view that file graphically as an SVG with:

google-pprof --web main.out prof.out

which gives as a familiar call graph like other tools, but with the clunky unit of number of samples rather than seconds.

Alternatively, we can also get some textual data with:

google-pprof --text main.out prof.out

which gives:

Using local file main.out.
Using local file prof.out.
Total: 187 samples
     187 100.0% 100.0%      187 100.0% common
       0   0.0% 100.0%      187 100.0% __libc_start_main
       0   0.0% 100.0%      187 100.0% _start
       0   0.0% 100.0%        4   2.1% fast
       0   0.0% 100.0%      187 100.0% main
       0   0.0% 100.0%      183  97.9% maybe_slow

See also: How to use google perf tools

Instrument your code with raw perf_event_open syscalls

I think this is the same underlying subsystem that perf uses, but you could of course attain even greater control by explicitly instrumenting your program at compile time with events of interest.

This is likely too hardcore for most people, but it's kind of fun. Minimal runnable example at: Quick way to count number of instructions executed in a C program

Intel VTune

https://en.wikipedia.org/wiki/VTune

This seems to be closed source and x86-only, but it is likely to be amazing from what I've heard. I'm not sure how free it is to use, but it seems to be free to download. TODO evaluate.

Tested in Ubuntu 18.04, gprof2dot 2019.11.30, valgrind 3.13.0, perf 4.15.18, Linux kernel 4.15.0, FLameGraph 1a0dc6985aad06e76857cf2a354bd5ba0c9ce96b, gperftools 2.5-2.


来源: stackexchange

问:
Attempt to call getDuration without a valid mediaplayer in media player on android
I am playing a song from sd card with seek bar. while getting the duration of the song I get Attempt to call getDuration without a valid mediaplayer and illegal state exception. the filename and path is valid one.I tried a long time I could not get a solution. and how to move seek bar while playing song.please help me.
my code:

```<code>Button play,pause,stop;
SeekBar seek;
MediaPlayer mediaPlayer;
SurfaceView sv;
boolean isPlaying = false;

    play.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            playsong(filename);
        }
    });

private void playsong(String filename2) {
    try{
        Log.e("filename2",filename2);
        mediaPlayer = new  MediaPlayer();
        mediaPlayer.setDataSource(filename2);
        seek.setMax(mediaPlayer.getDuration());
        mediaPlayer.prepare();
        mediaPlayer.start();
        myHandler.post(runn);
        isPlaying = true;
        mediaPlayer.setOnCompletionListener(this);
    }
    catch(Exception ex){
        Log.e("sdcard-err2:",""+ex);
    }

}

private Handler myHandler = new Handler();
    final Runnable runn = new Runnable()
    {
        public void run()
        {
            if (mediaPlayer != null)
            {
                if (isPlaying )
                {
                    try
                    {
                       int currentPosition = mediaPlayer.getCurrentPosition();

                       seek.setProgress(currentPosition);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    sv.postDelayed(runn, 150);
                }
            }
        }

    };
</code>```

my log cat errors:

```<code>05-17 09:46:43.578: ERROR/filename2(1670): /sdcard/The Dance Theme.mp3
05-17 09:46:43.594: ERROR/MediaPlayer(1670): Attempt to call getDuration without a valid mediaplayer
05-17 09:46:43.594: ERROR/MediaPlayer(1670): error (-38, 0)
05-17 09:46:43.594: ERROR/MediaPlayer(1670): prepareAsync called in state 0
05-17 09:46:43.594: ERROR/sdcard-err2:(1670): java.lang.IllegalStateException
05-17 09:46:43.594: ERROR/MediaPlayer(1670): Error (-38,0)
</code>```

答: You might be calling getDuration before the file is fully loaded. See if the solution to this question works for you.</p>

ContentLicense: CC BY-SA 4.0

Body:
I am playing a song from sd card with seek bar. while getting the duration of the song I get Attempt to call getDuration without a valid mediaplayer and illegal state exception. the filename and path is valid one.I tried a long time I could not get a solution. and how to move seek bar while playing song.please help me.
my code:

Button play,pause,stop;
SeekBar seek;
MediaPlayer mediaPlayer;
SurfaceView sv;
boolean isPlaying = false;

    play.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            playsong(filename);
        }
    });

private void playsong(String filename2) {
    try{
        Log.e("filename2",filename2);
        mediaPlayer = new  MediaPlayer();
        mediaPlayer.setDataSource(filename2);
        seek.setMax(mediaPlayer.getDuration());
        mediaPlayer.prepare();
        mediaPlayer.start();
        myHandler.post(runn);
        isPlaying = true;
        mediaPlayer.setOnCompletionListener(this);
    }
    catch(Exception ex){
        Log.e("sdcard-err2:",""+ex);
    }

}

private Handler myHandler = new Handler();
    final Runnable runn = new Runnable()
    {
        public void run()
        {
            if (mediaPlayer != null)
            {
                if (isPlaying )
                {
                    try
                    {
                       int currentPosition = mediaPlayer.getCurrentPosition();

                       seek.setProgress(currentPosition);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    sv.postDelayed(runn, 150);
                }
            }
        }

    };

my log cat errors:

05-17 09:46:43.578: ERROR/filename2(1670): /sdcard/The Dance Theme.mp3
05-17 09:46:43.594: ERROR/MediaPlayer(1670): Attempt to call getDuration without a valid mediaplayer
05-17 09:46:43.594: ERROR/MediaPlayer(1670): error (-38, 0)
05-17 09:46:43.594: ERROR/MediaPlayer(1670): prepareAsync called in state 0
05-17 09:46:43.594: ERROR/sdcard-err2:(1670): java.lang.IllegalStateException
05-17 09:46:43.594: ERROR/MediaPlayer(1670): Error (-38,0)

ContentLicense: CC BY-SA 3.0


问:
Running Docker with Postgres FATAL: cache lookup failed
I'm trying to run Docker with a Postgres database. The part of my docker-compose.yml looks like this:

```<code>db:
    image: postgres
    restart: always
    ports:
      - "5432:5432"
    environment:
      TZ: Europe/Berlin
      POSTGRES_DB: my-db
      POSTGRES_USER: my-db-user
      POSTGRES_PASSWORD: a23Kwum1Ql
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - main
</code>```

When I run docker-compose up in the terminal, I get the error FATAL:  cache lookup failed for access method 403.

I tried to suppress the errors by running docker-compose up -d and then had a look at the database folders by running first docker exec -i myproject-master_db_1 bash and then ls /etc/postgresql. Result: The directory is empty which it shouldn't be to the best of my knowledge.

My database does not contain any important data, so I'd also be happy about solutions completely resetting the database (which I also tried but obviously failed).

答:
Your volume must be absolute, like ``/app/to/your/db-data`.

environment replace ':' by "="

Then make sure you got the right access on the volume/folder when running docker-compose

```<code>db:
    image: postgres
    restart: always
    ports:
      - "5432:5432"
    privilied: true                 # Add this line
    environment:
      - TZ=Europe/Berlin              # replace ':' to "="
      - POSTGRES_DB=my-db             # replace ':' to "="
      - POSTGRES_USER=my-db-user      # replace ':' to "="
      - POSTGRES_PASSWORD=a23Kwum1Ql  # replace ':' to "="
    volumes:
      - /app/to/your/db-data:/var/lib/postgresql/data # Update this line
    networks:
      - main
</code>```

Body:
I'm trying to run Docker with a Postgres database. The part of my docker-compose.yml looks like this:

db:
    image: postgres
    restart: always
    ports:
      - "5432:5432"
    environment:
      TZ: Europe/Berlin
      POSTGRES_DB: my-db
      POSTGRES_USER: my-db-user
      POSTGRES_PASSWORD: a23Kwum1Ql
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - main

When I run docker-compose up in the terminal, I get the error FATAL:  cache lookup failed for access method 403.

I tried to suppress the errors by running docker-compose up -d and then had a look at the database folders by running first docker exec -i myproject-master_db_1 bash and then ls /etc/postgresql. Result: The directory is empty which it shouldn't be to the best of my knowledge.

My database does not contain any important data, so I'd also be happy about solutions completely resetting the database (which I also tried but obviously failed).

Body:
Your volume must be absolute, like ``/app/to/your/db-data`.

environment replace ':' by "="

Then make sure you got the right access on the volume/folder when running docker-compose

db:
    image: postgres
    restart: always
    ports:
      - "5432:5432"
    privilied: true                 # Add this line
    environment:
      - TZ=Europe/Berlin              # replace ':' to "="
      - POSTGRES_DB=my-db             # replace ':' to "="
      - POSTGRES_USER=my-db-user      # replace ':' to "="
      - POSTGRES_PASSWORD=a23Kwum1Ql  # replace ':' to "="
    volumes:
      - /app/to/your/db-data:/var/lib/postgresql/data # Update this line
    networks:
      - main


问:
Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)
I am having a big problem trying to connect to mysql. When I run:

```<code>/usr/local/mysql/bin/mysql start
</code>```

I have the following error :

```<code>Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)
</code>```

I do have mysql.sock under the /var/mysql directory.

In /etc/my.cnf I have:

```<code>[client]
port=3306
socket=/var/mysql/mysql.sock

[mysqld]
port=3306
socket=/var/mysql/mysql.sock
key_buffer_size=16M
max_allowed_packet=8M
</code>```

and in /etc/php.ini I have :

```<code>; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
mysql.default_socket = /var/mysql/mysql.sock
</code>```

I have restarted apache using sudo /opt/local/apache2/bin/apachectl restart

But I still have the error.

Otherwise, I don't know if that's relevant but when I do mysql_config --sockets I get

```<code>--socket         [/tmp/mysql.sock]
</code>```

答: I solved by deleting tc.log into /var/lib/mysql directory because it was corrupdet after full disk. Than, when i started mysql, the log file was created as new.  </p>

Body:
I am having a big problem trying to connect to mysql. When I run:

/usr/local/mysql/bin/mysql start

I have the following error :

Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)

I do have mysql.sock under the /var/mysql directory.

In /etc/my.cnf I have:

[client]
port=3306
socket=/var/mysql/mysql.sock

[mysqld]
port=3306
socket=/var/mysql/mysql.sock
key_buffer_size=16M
max_allowed_packet=8M

and in /etc/php.ini I have :

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
mysql.default_socket = /var/mysql/mysql.sock

I have restarted apache using sudo /opt/local/apache2/bin/apachectl restart

But I still have the error.

Otherwise, I don't know if that's relevant but when I do mysql_config --sockets I get

--socket         [/tmp/mysql.sock]


问:
Java: Removing content from Json file
I wonder how to remove/empty an .json File.

I have found various Methods online, how to empty an JSON Objekt, however the Objekt that were given were defined and done in the very same file

What i want: To acces a .json File and delete all the content/empty the objekt.

How can i achieve this?
Filepath

EDIT
@Sagar Kharab
the Errors

答: You may simply truncate the file or overwrite it with "{}".</p>

Title: Java: Removing content from Json file


问:
RESTful servive : Unable to return array of object using json
This sample was extracted from one of the many samples found via Google.
The application returns 3 objects but all attributes are NULL.

The following is the Controller Object.

```<code>import java.net.URI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

@RestController
@RequestMapping(path = "/employees")
public class EmployeeController
{
    @Autowired
    private EmployeeDAO employeeDao;

    @GetMapping(path="/", produces = "application/json")
    public Employees getEmployees()
    {
        return employeeDao.getAllEmployees();
    }

    @PostMapping(path= "/", consumes = "application/json", produces = "application/json")
    public ResponseEntity<Object> addEmployee(@RequestBody Employee employee)
    {
        Integer id = employeeDao.getAllEmployees().getEmployeeList().size() + 1;
        employee.setId(id);

        employeeDao.addEmployee(employee);

        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                                    .path("/{id}")
                                    .buildAndExpand(employee.getId())
                                    .toUri();

        return ResponseEntity.created(location).build();
    }
}
</code>```

Here is the Employees object which gets returned via the getEmployees call.

```<code>import java.util.ArrayList;
import java.util.List;

public class Employees
{
    private List<Employee> employeeList;

    public List<Employee> getEmployeeList() {
        if(employeeList == null) {
            employeeList = new ArrayList<>();
        }
        return employeeList;
    }

    public void setEmployeeList(List<Employee> employeeList) {
        this.employeeList = employeeList;
    }
}
</code>```

In case you need it, here is the DAO.

```<code>import org.springframework.stereotype.Repository;

@Repository
public class EmployeeDAO
{
    private static Employees list = new Employees();

    static
    {
        list.getEmployeeList().add(new Employee(11, "Lokesh", "Gupta", "howtodoinjava@gmail.com"));
        list.getEmployeeList().add(new Employee(22, "Alex", "Kolenchiskey", "abc@gmail.com"));
        list.getEmployeeList().add(new Employee(33, "David", "Kameron", "titanic@gmail.com"));
    }

    public Employees getAllEmployees()
    {
        return list;
    }

    public void addEmployee(Employee employee) {
        list.getEmployeeList().add(employee);
    }
}
</code>```

As requested, here is the Employee class.

```<code>import javax.annotation.ManagedBean;

import org.springframework.web.context.annotation.ApplicationScope;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

//@ManagedBean
//@ApplicationScope
//@Data

public class Employee {

    public Employee() {

    }
    public Employee(Integer id, String firstName, String lastName, String email) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    @Setter  @Getter  private Integer id;
    private String firstName;
    private String lastName;
    private String email;

    //Getters and setters

    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
    }
}
</code>```

Here is the partial client code....

```<code>    private static void getEmployees()
    {

        // HttpHeaders
        HttpHeaders headers = new HttpHeaders();

        headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
        // Request to return JSON format
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("my_other_key", "my_other_value");

        // HttpEntity<String>: To get result as String.
        HttpEntity<String> entity = new HttpEntity<String>(headers);

        // RestTemplate
        RestTemplate restTemplate = new RestTemplate();

        // Send request with GET method, and Headers.
        ///This works but only prints IDs.
        ResponseEntity<String> response = restTemplate.exchange(URL_EMPLOYEES, HttpMethod.GET, entity, String.class);
        String result = response.getBody();
        System.out.println(result);

        HttpEntity<Employees> entity2 = new HttpEntity<Employees>(headers);
      ResponseEntity<Employees> eResponse = restTemplate.exchange(URL_EMPLOYEES, HttpMethod.GET, entity2, Employees.class);
      List <Employee> eList = eResponse.getBody().getEmployeeList();
      for (Employee e : eList)
      {
          System.out.println(e.toString());
      }
   }
</code>```

From the above, the following call works and prints out the 3 IDs.

```<code>    ResponseEntity<String> response = restTemplate.exchange(URL_EMPLOYEES, HttpMethod.GET, entity, String.class);
    String result = response.getBody();
    System.out.println(result);
</code>```

Once the IDs were returned, the next step was to print out all the attributes for each employee using the next set of calls.

```<code>  ResponseEntity<Employees> eResponse = restTemplate.exchange(URL_EMPLOYEES, HttpMethod.GET, entity, Employees.class);
  List <Employee> eList = eResponse.getBody().getEmployeeList();
  for (Employee e : eList)
  {
      System.out.println(e.toString());
  }
</code>```

I placed a break point on the line doing the LIST call and verified the eResponse object has 3 items in the List BUT all 3 items show each of their associated attributes to be NULL.

Since I confirmed the call seems to be working since the IDs are returned in the first call, what command or setup is missing for the 2nd call to allow for the complete objects to be returned?

答:
Here are the 3 important pieces changed to get this working.

1 - Server Side - nothing really changed with this procedure.

```<code>@GetMapping(path="/", produces = "application/json")
public Employees getEmployees()
{
    return employeeDao.getAllEmployees();
}
</code>```

2 - Client side - simplified it.

```<code>private static void getEmployees()
{

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("my_other_key", "my_other_value");
    HttpEntity<Employee> entity = new HttpEntity<>(null,headers);
    ResponseEntity<String>result = restTemplate.exchange(URL_EMPLOYEES, HttpMethod.GET, entity, String.class);
    System.out.println(result);
 }
</code>```

3 - Server Side - this was the key to getting it working.

Originally the Employee.java looked like this.

```<code>@Setter  @Getter private Integer id;
private String firstName;
private String lastName;
private String email;
</code>```

Once i change to the following, it worked.

```<code>@Setter  @Getter private Integer id;
@Setter  @Getter private String firstName;
@Setter  @Getter private String lastName;
@Setter  @Getter private String email;
</code>```

Still not 100% of what is really going on but it works.

Body:
This sample was extracted from one of the many samples found via Google.
The application returns 3 objects but all attributes are NULL.

The following is the Controller Object.

import java.net.URI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

@RestController
@RequestMapping(path = "/employees")
public class EmployeeController
{
    @Autowired
    private EmployeeDAO employeeDao;

    @GetMapping(path="/", produces = "application/json")
    public Employees getEmployees()
    {
        return employeeDao.getAllEmployees();
    }

    @PostMapping(path= "/", consumes = "application/json", produces = "application/json")
    public ResponseEntity<Object> addEmployee(@RequestBody Employee employee)
    {
        Integer id = employeeDao.getAllEmployees().getEmployeeList().size() + 1;
        employee.setId(id);

        employeeDao.addEmployee(employee);

        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                                    .path("/{id}")
                                    .buildAndExpand(employee.getId())
                                    .toUri();

        return ResponseEntity.created(location).build();
    }
}

Here is the Employees object which gets returned via the getEmployees call.

import java.util.ArrayList;
import java.util.List;

public class Employees
{
    private List<Employee> employeeList;

    public List<Employee> getEmployeeList() {
        if(employeeList == null) {
            employeeList = new ArrayList<>();
        }
        return employeeList;
    }

    public void setEmployeeList(List<Employee> employeeList) {
        this.employeeList = employeeList;
    }
}

In case you need it, here is the DAO.

import org.springframework.stereotype.Repository;

@Repository
public class EmployeeDAO
{
    private static Employees list = new Employees();

    static
    {
        list.getEmployeeList().add(new Employee(11, "Lokesh", "Gupta", "howtodoinjava@gmail.com"));
        list.getEmployeeList().add(new Employee(22, "Alex", "Kolenchiskey", "abc@gmail.com"));
        list.getEmployeeList().add(new Employee(33, "David", "Kameron", "titanic@gmail.com"));
    }

    public Employees getAllEmployees()
    {
        return list;
    }

    public void addEmployee(Employee employee) {
        list.getEmployeeList().add(employee);
    }
}

As requested, here is the Employee class.

import javax.annotation.ManagedBean;

import org.springframework.web.context.annotation.ApplicationScope;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

//@ManagedBean
//@ApplicationScope
//@Data

public class Employee {

    public Employee() {

    }
    public Employee(Integer id, String firstName, String lastName, String email) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    @Setter  @Getter  private Integer id;
    private String firstName;
    private String lastName;
    private String email;

    //Getters and setters

    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
    }
}

Here is the partial client code....

    private static void getEmployees()
    {

        // HttpHeaders
        HttpHeaders headers = new HttpHeaders();

        headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
        // Request to return JSON format
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("my_other_key", "my_other_value");

        // HttpEntity<String>: To get result as String.
        HttpEntity<String> entity = new HttpEntity<String>(headers);

        // RestTemplate
        RestTemplate restTemplate = new RestTemplate();

        // Send request with GET method, and Headers.
        ///This works but only prints IDs.
        ResponseEntity<String> response = restTemplate.exchange(URL_EMPLOYEES, HttpMethod.GET, entity, String.class);
        String result = response.getBody();
        System.out.println(result);

        HttpEntity<Employees> entity2 = new HttpEntity<Employees>(headers);
      ResponseEntity<Employees> eResponse = restTemplate.exchange(URL_EMPLOYEES, HttpMethod.GET, entity2, Employees.class);
      List <Employee> eList = eResponse.getBody().getEmployeeList();
      for (Employee e : eList)
      {
          System.out.println(e.toString());
      }
   }

From the above, the following call works and prints out the 3 IDs.

    ResponseEntity<String> response = restTemplate.exchange(URL_EMPLOYEES, HttpMethod.GET, entity, String.class);
    String result = response.getBody();
    System.out.println(result);

Once the IDs were returned, the next step was to print out all the attributes for each employee using the next set of calls.

  ResponseEntity<Employees> eResponse = restTemplate.exchange(URL_EMPLOYEES, HttpMethod.GET, entity, Employees.class);
  List <Employee> eList = eResponse.getBody().getEmployeeList();
  for (Employee e : eList)
  {
      System.out.println(e.toString());
  }

I placed a break point on the line doing the LIST call and verified the eResponse object has 3 items in the List BUT all 3 items show each of their associated attributes to be NULL.

Since I confirmed the call seems to be working since the IDs are returned in the first call, what command or setup is missing for the 2nd call to allow for the complete objects to be returned?

Body:
Here are the 3 important pieces changed to get this working.

1 - Server Side - nothing really changed with this procedure.

@GetMapping(path="/", produces = "application/json")
public Employees getEmployees()
{
    return employeeDao.getAllEmployees();
}

2 - Client side - simplified it.

private static void getEmployees()
{

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("my_other_key", "my_other_value");
    HttpEntity<Employee> entity = new HttpEntity<>(null,headers);
    ResponseEntity<String>result = restTemplate.exchange(URL_EMPLOYEES, HttpMethod.GET, entity, String.class);
    System.out.println(result);
 }

3 - Server Side - this was the key to getting it working.

Originally the Employee.java looked like this.

@Setter  @Getter private Integer id;
private String firstName;
private String lastName;
private String email;

Once i change to the following, it worked.

@Setter  @Getter private Integer id;
@Setter  @Getter private String firstName;
@Setter  @Getter private String lastName;
@Setter  @Getter private String email;

Still not 100% of what is really going on but it works.


问:
Postgresql - what does % and <%> mean?
I'm trying to explore RDkit cartridge on Postgresql. I've installed RDkit on Postgresql and now am trying to go through the documentation at http://rdkit.org/docs_temp/Cartridge.html. Here when trying to understand the similarity search, I've come across the below code. But I'm unable to understand it fully as I'm totally new to both Postgresql and RDkit.

```<code>chembl_23=# create or replace function get_mfp2_neighbors(smiles text)
    returns table(molregno integer, m mol, similarity double precision) as
  $$
  select molregno,m,tanimoto_sml(morganbv_fp(mol_from_smiles($1::cstring)),mfp2) as similarity
  from rdk.fps join rdk.mols using (molregno)
  where morganbv_fp(mol_from_smiles($1::cstring))%mfp2
  order by morganbv_fp(mol_from_smiles($1::cstring))<%>mfp2;
  $$ language sql stable ;
</code>```

My questions are -

1) What does %mfp2 mean in WHERE clause? - I've searched on Google and stumbled upon pages where it's mentioned that it's LIKE %% operator in SQL. But here LIKE operator is not used, still is the meaning same?

2) What does <%>mfp2 mean in ORDER BY clause? - I'm clueless about this operator.

Can anyone please help me understand this?

答:
Those operators are defined by RDkit, they have no inherent meaning to PostgreSQL itself (with the datatypes being used). You will find them documented within the context of RDkit on that page you linked to.  Other extensions have operators with the same "spelling" (but different argument types) but used in other ways.

%: operator used for similarity searches using Tanimoto similarity.
  Returns whether or not the Tanimoto similarity between two
  fingerprints (either two sfp or two bfp values) exceeds
  rdkit.tanimoto_threshold.

<%>:
  used for Tanimoto KNN searches (to return ordered lists of neighbors).

mfp2 is the name of a column, so %mfp2 would be more clearly written with space someconstant % mfp2

Title: Postgresql - what does % and <%> mean?

Body:
I'm trying to explore RDkit cartridge on Postgresql. I've installed RDkit on Postgresql and now am trying to go through the documentation at http://rdkit.org/docs_temp/Cartridge.html. Here when trying to understand the similarity search, I've come across the below code. But I'm unable to understand it fully as I'm totally new to both Postgresql and RDkit.

chembl_23=# create or replace function get_mfp2_neighbors(smiles text)
    returns table(molregno integer, m mol, similarity double precision) as
  $$
  select molregno,m,tanimoto_sml(morganbv_fp(mol_from_smiles($1::cstring)),mfp2) as similarity
  from rdk.fps join rdk.mols using (molregno)
  where morganbv_fp(mol_from_smiles($1::cstring))%mfp2
  order by morganbv_fp(mol_from_smiles($1::cstring))<%>mfp2;
  $$ language sql stable ;

My questions are -

1) What does %mfp2 mean in WHERE clause? - I've searched on Google and stumbled upon pages where it's mentioned that it's LIKE %% operator in SQL. But here LIKE operator is not used, still is the meaning same?

2) What does <%>mfp2 mean in ORDER BY clause? - I'm clueless about this operator.

Can anyone please help me understand this?


问:
check username with bvalidator
Why the return value of my function always false

this is my function:

```<code>function checkUsername(username) {
    var ret=false;
    $.ajax({
        type: "POST",
        url: "user_validate.php",
        async: true,
        data: "username="+username,
        success: function(data){
            if(data == 0){
                ret = true;
            }else{
               ret = false;
            }
        },
        error:function() {
            alert("ERROR");
        }
    });
    return ret;
}
</code>```

Is var ret doesn't set the value?
Anything wrong with it..?
thanks before..!

答:
You're a little confused on the scope of things here. The ajax call is asynchronous, so by the time it answers, it no longer has anything to do with the function it was called from. What you really want to do is either set up some sort of event listener or a (my preferred method) callback.

Also, do yourself a favor and use the shortened syntax $.post(): http://api.jquery.com/jQuery.post/

```<code>function checkUsername(username) {
    $.post("user_validate.php", "username=" + username, function(data) {
        if (data == 0) {
            // Update DOM or element, or whatever you want to do
        } else {
            // Handle negative result
        }
    });
}
</code>```

Title: check username with bvalidator

Body:
Why the return value of my function always false

this is my function:

function checkUsername(username) {
    var ret=false;
    $.ajax({
        type: "POST",
        url: "user_validate.php",
        async: true,
        data: "username="+username,
        success: function(data){
            if(data == 0){
                ret = true;
            }else{
               ret = false;
            }
        },
        error:function() {
            alert("ERROR");
        }
    });
    return ret;
}

Is var ret doesn't set the value?
Anything wrong with it..?
thanks before..!

Body:
You're a little confused on the scope of things here. The ajax call is asynchronous, so by the time it answers, it no longer has anything to do with the function it was called from. What you really want to do is either set up some sort of event listener or a (my preferred method) callback.

Also, do yourself a favor and use the shortened syntax $.post(): http://api.jquery.com/jQuery.post/

function checkUsername(username) {
    $.post("user_validate.php", "username=" + username, function(data) {
        if (data == 0) {
            // Update DOM or element, or whatever you want to do
        } else {
            // Handle negative result
        }
    });
}


问:
How do I setup the dotenv file in Node.js?
I am trying to use the dotenv NPM package and it is not working for me. I have a file config/config.js with the following content:

'use strict';

var dotenv = require('dotenv');
dotenv.load();
console.log('config');
```
<p>I have another file <code>.env</code> at the root of my application folder. I also have an environment variable <code>TWILIO_ACCOUNT_SID</code>.</p>
<p>This is the process I go through while trying to use the environment variables in a certain function:</p>
```$ node
> require('./config/config.js');
config
{}
> process.env.TWILIO_ACCOUNT_SID
undefined
```

I defined the TWILIO_ACCOUNT_SID in my .env file but as soon as I try to output the value in my console, I get an error stating that the variable is undefined.

I will be very grateful for any support in troubleshooting this issue.

答:
is dotenv installed in your project?

Try to install it using npm install dotenv in your project.

Once it is installed load it in any files where you need it using const env = require('dotenv').config().

You can then use in any line where you need to. For example to call port from .env use: process.env.PORT

Body:
I am trying to use the dotenv NPM package and it is not working for me. I have a file config/config.js with the following content:

'use strict';

var dotenv = require('dotenv');
dotenv.load();
console.log('config');

I have another file .env at the root of my application folder. I also have an environment variable TWILIO_ACCOUNT_SID.

This is the process I go through while trying to use the environment variables in a certain function:

$ node
> require('./config/config.js');
config
{}
> process.env.TWILIO_ACCOUNT_SID
undefined

I defined the TWILIO_ACCOUNT_SID in my .env file but as soon as I try to output the value in my console, I get an error stating that the variable is undefined.

I will be very grateful for any support in troubleshooting this issue.


问:
How to Check If Some Futures in a Collection Have Failed
I used traverse to execute a collection of futures like this:

val result: Future[List[Either[Error, Int]]] = Future.traverse(urls)(foo(_))
```

<p>I end up with a <code>Future[List[Either[Error, Int]]]</code>. How can I check that one of these futures resulted in an Error?</p>

<p>I tried to do this but I think it is wrong because I am reading that you cannot substitute variables for futures?</p>

<pre class="lang-scala prettyprint-override"><code>    val check: Future[Boolean] = result.map{
      fut => fut.exists(c => c.isLeft)
    }

    check.map{
      b => b match {
        case true => // do something
        case false => // do something
      }
    }
</code>```

答:
You can convert the result to a list of errors like this:

```<code>val errors: Future[List[Error]] = result.map(_.collect{ case Left(err) => err })
</code>```

It is then possible to use Await.result to extract these error values, but that is nearly always a bad idea because it blocks the current thread.

It is better to ask "What do I want to do once the Future is complete but returns errors?". Then implement that behaviour in a map or foreach on the errors Future.

Body:
I used traverse to execute a collection of futures like this:

val result: Future[List[Either[Error, Int]]] = Future.traverse(urls)(foo(_))

I end up with a Future[List[Either[Error, Int]]]. How can I check that one of these futures resulted in an Error?

I tried to do this but I think it is wrong because I am reading that you cannot substitute variables for futures?

    val check: Future[Boolean] = result.map{
      fut => fut.exists(c => c.isLeft)
    }

    check.map{
      b => b match {
        case true => // do something
        case false => // do something
      }
    }

Body:
You can convert the result to a list of errors like this:

val errors: Future[List[Error]] = result.map(_.collect{ case Left(err) => err })

It is then possible to use Await.result to extract these error values, but that is nearly always a bad idea because it blocks the current thread.

It is better to ask "What do I want to do once the Future is complete but returns errors?". Then implement that behaviour in a map or foreach on the errors Future.


问:
How to ignore SwiftUI modal background, or make modal background clear/transparent?
Currently we get a free opaque white/black background for swiftUI modal. Is there anyway to remove the free opaque color and make the modal view transparent?

In the image below, the end result should be able to see the image even with modal presented.

答:
based on this code snippet you can create viewcontroller extension and modify your presentation.
here is modified code:

 struct ViewControllerHolder {
    weak var value: UIViewController?
    init(_ value: UIViewController?) {
        self.value = value
    }
}

struct ViewControllerKey: EnvironmentKey {
    static var defaultValue: ViewControllerHolder { return ViewControllerHolder(UIApplication.shared.windows.first?.rootViewController ) }
}

extension EnvironmentValues {
    var viewController: ViewControllerHolder {
        get { return self[ViewControllerKey.self] }
        set { self[ViewControllerKey.self] = newValue }
    }
}

extension UIViewController {
    func present<Content: View>(presentationStyle: UIModalPresentationStyle = .automatic, transitionStyle: UIModalTransitionStyle = .coverVertical, animated: Bool = true, completion: @escaping () -> Void = {}, @ViewBuilder builder: () -> Content) {
        let toPresent = UIHostingController(rootView: AnyView(EmptyView()))
        toPresent.modalPresentationStyle = presentationStyle
        toPresent.rootView = AnyView(
            builder()
                .environment(\.viewController, ViewControllerHolder(toPresent))
        )

        toPresent.view.backgroundColor = .clear // This line is modified
        self.present(toPresent, animated: animated, completion: completion)
    }
}

```

<p>Your SwiftUI ContentView:</p>

<pre class="lang-swift prettyprint-override"><code>struct ContentView: View {

    @Environment(\.viewController) private var viewControllerHolder: ViewControllerHolder
    private var viewController: UIViewController? {
        self.viewControllerHolder.value
    }

    var body: some View {
        ZStack {
            Color.red
            Button(action: {
                self.viewController?.present(builder: {
                    Text("OK")
                })
            }) {
               Text("Present me!")
            }
        }
    }
}
</code>```

Body:
based on this code snippet you can create viewcontroller extension and modify your presentation.
here is modified code:

 struct ViewControllerHolder {
    weak var value: UIViewController?
    init(_ value: UIViewController?) {
        self.value = value
    }
}

struct ViewControllerKey: EnvironmentKey {
    static var defaultValue: ViewControllerHolder { return ViewControllerHolder(UIApplication.shared.windows.first?.rootViewController ) }
}

extension EnvironmentValues {
    var viewController: ViewControllerHolder {
        get { return self[ViewControllerKey.self] }
        set { self[ViewControllerKey.self] = newValue }
    }
}

extension UIViewController {
    func present<Content: View>(presentationStyle: UIModalPresentationStyle = .automatic, transitionStyle: UIModalTransitionStyle = .coverVertical, animated: Bool = true, completion: @escaping () -> Void = {}, @ViewBuilder builder: () -> Content) {
        let toPresent = UIHostingController(rootView: AnyView(EmptyView()))
        toPresent.modalPresentationStyle = presentationStyle
        toPresent.rootView = AnyView(
            builder()
                .environment(\.viewController, ViewControllerHolder(toPresent))
        )

        toPresent.view.backgroundColor = .clear // This line is modified
        self.present(toPresent, animated: animated, completion: completion)
    }
}

Your SwiftUI ContentView:

struct ContentView: View {

    @Environment(\.viewController) private var viewControllerHolder: ViewControllerHolder
    private var viewController: UIViewController? {
        self.viewControllerHolder.value
    }

    var body: some View {
        ZStack {
            Color.red
            Button(action: {
                self.viewController?.present(builder: {
                    Text("OK")
                })
            }) {
               Text("Present me!")
            }
        }
    }
}


问:
Java.io.IOException: Not connected will be reported when sending SMS using openSMPP lib
```<code>@RequestMapping(method = RequestMethod.GET, value = "/test")
@ResponseBody
@ApiOperation(httpMethod = "GET", value = "测试apo", notes = "")
public void testMail(@RequestParam(name = "host", required = false) String host,
                     @RequestParam(name = "port", required = false) String port,
                     @RequestParam(name = "userName", required = false) String userName,
                     @RequestParam(name = "password", required = false) String password,
                     @RequestParam(name = "phonenumber", required = false) String phonenumber,
                     @RequestParam(name = "verifyCode", required = false) String verifyCode
) {
    log.info("start to send sms notification, reciever,host {},port {}, userName {} password {} destinations is {} verifyCode {}", host, port, userName, password, phonenumber, verifyCode);
    try {
        TCPIPConnection connection = new TCPIPConnection(host, Integer.parseInt(port));
        Session session = new Session(connection);
        BindRequest request = new BindTransmitter();
        request.setSystemId(userName);
        request.setPassword(password);
        //SMPP protocol version
        request.setInterfaceVersion((byte) 0x34);
        session.bind(request);
        String content = "[Registration]" + verifyCode + " is your verification code. Valid in 15 minutes. Please do not share this code with anyone else.";
        SubmitSM submitSM = constructRequest(phonenumber, content);
        SubmitSMResp response = session.submit(submitSM);
        String result = new String(response.toString());
        log.info("send sms notification result is {}", result);
    } catch (Exception e) {
        log.error("invoke sms session exception", e);
    }

}

private SubmitSM constructRequest(String phoneNumber,String content) throws WrongLengthOfStringException, UnsupportedEncodingException {
    String recipientPhoneNumber = phoneNumber;

    SubmitSM request = new SubmitSM();
    request.setDestAddr(createAddress(recipientPhoneNumber));
    request.setShortMessage(content,Data.ENC_UTF8);
    request.setReplaceIfPresentFlag((byte) 0);
    request.setEsmClass((byte) 0);
    request.setProtocolId((byte) 0);
    request.setPriorityFlag((byte) 0);
    request.setRegisteredDelivery((byte) 1);// we want delivery reports
    request.setDataCoding((byte) 0);
    request.setSmDefaultMsgId((byte) 0);
    return request;
}

private Address createAddress(String address) throws WrongLengthOfStringException {
    Address addressInst = new Address();
    // national ton
    addressInst.setTon((byte) 5);
    // numeric plan indicator
    addressInst.setNpi((byte) 0);
    addressInst.setAddress(address, Data.SM_ADDR_LEN);
    return addressInst;
}
</code>```

Here is the exception message

```<code>java.io.IOException: Not connected
    at org.smpp.TCPIPConnection.send(TCPIPConnection.java:340) ~[opensmpp-core-3.0.2.jar!/:na]
    at org.smpp.Transmitter.send(Transmitter.java:69) ~[opensmpp-core-3.0.2.jar!/:na]
    at org.smpp.Session.send(Session.java:937) ~[opensmpp-core-3.0.2.jar!/:na]
    at org.smpp.Session.send(Session.java:990) ~[opensmpp-core-3.0.2.jar!/:na]
    at org.smpp.Session.submit(Session.java:591) ~[opensmpp-core-3.0.2.jar!/:na]
    at com.example.mq.controller.SmsController.testMail(SmsController.java:45) ~[classes!/:0.0.1-SNAPSHOT]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
</code>```

答: I looked at the source code of openSMPP to understand the reason, because the account binding failed, session will be closed; which will cause a exception when executing session.submit

Body:
@RequestMapping(method = RequestMethod.GET, value = "/test")
@ResponseBody
@ApiOperation(httpMethod = "GET", value = "测试apo", notes = "")
public void testMail(@RequestParam(name = "host", required = false) String host,
                     @RequestParam(name = "port", required = false) String port,
                     @RequestParam(name = "userName", required = false) String userName,
                     @RequestParam(name = "password", required = false) String password,
                     @RequestParam(name = "phonenumber", required = false) String phonenumber,
                     @RequestParam(name = "verifyCode", required = false) String verifyCode
) {
    log.info("start to send sms notification, reciever,host {},port {}, userName {} password {} destinations is {} verifyCode {}", host, port, userName, password, phonenumber, verifyCode);
    try {
        TCPIPConnection connection = new TCPIPConnection(host, Integer.parseInt(port));
        Session session = new Session(connection);
        BindRequest request = new BindTransmitter();
        request.setSystemId(userName);
        request.setPassword(password);
        //SMPP protocol version
        request.setInterfaceVersion((byte) 0x34);
        session.bind(request);
        String content = "[Registration]" + verifyCode + " is your verification code. Valid in 15 minutes. Please do not share this code with anyone else.";
        SubmitSM submitSM = constructRequest(phonenumber, content);
        SubmitSMResp response = session.submit(submitSM);
        String result = new String(response.toString());
        log.info("send sms notification result is {}", result);
    } catch (Exception e) {
        log.error("invoke sms session exception", e);
    }

}

private SubmitSM constructRequest(String phoneNumber,String content) throws WrongLengthOfStringException, UnsupportedEncodingException {
    String recipientPhoneNumber = phoneNumber;

    SubmitSM request = new SubmitSM();
    request.setDestAddr(createAddress(recipientPhoneNumber));
    request.setShortMessage(content,Data.ENC_UTF8);
    request.setReplaceIfPresentFlag((byte) 0);
    request.setEsmClass((byte) 0);
    request.setProtocolId((byte) 0);
    request.setPriorityFlag((byte) 0);
    request.setRegisteredDelivery((byte) 1);// we want delivery reports
    request.setDataCoding((byte) 0);
    request.setSmDefaultMsgId((byte) 0);
    return request;
}

private Address createAddress(String address) throws WrongLengthOfStringException {
    Address addressInst = new Address();
    // national ton
    addressInst.setTon((byte) 5);
    // numeric plan indicator
    addressInst.setNpi((byte) 0);
    addressInst.setAddress(address, Data.SM_ADDR_LEN);
    return addressInst;
}

Here is the exception message

java.io.IOException: Not connected
    at org.smpp.TCPIPConnection.send(TCPIPConnection.java:340) ~[opensmpp-core-3.0.2.jar!/:na]
    at org.smpp.Transmitter.send(Transmitter.java:69) ~[opensmpp-core-3.0.2.jar!/:na]
    at org.smpp.Session.send(Session.java:937) ~[opensmpp-core-3.0.2.jar!/:na]
    at org.smpp.Session.send(Session.java:990) ~[opensmpp-core-3.0.2.jar!/:na]
    at org.smpp.Session.submit(Session.java:591) ~[opensmpp-core-3.0.2.jar!/:na]
    at com.example.mq.controller.SmsController.testMail(SmsController.java:45) ~[classes!/:0.0.1-SNAPSHOT]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]


问:
Finding Specific Text Records
I am working on a project with a table having a text field as its Primary Key.  On on form, I have the [Name] listed in a list of items and want to open the corresponding record to that specific text record.  Here is what I am looking at:

Any help is greatly appreciated!

Sincerely,

Kevin

答:
This will open a form with the Muscles button:

Open Properties on the Muscles button and go to the On Click event on the Event tab. Click the three dots and choose Code Builder.

Add the following code (change frmMuscles to the name of the form you are opening, MuscleName to the name of the field, and sfrmMuscles to the name of the subform):

docmd.OpenForm "frmMuscles",,,"MuscleName='" & me.sfrmMuscles.MuscleName.Value & "'"

Note that you need to point to the subform that has the name of the muscle desired to be opened and it needs to be surrounded by single quotes which are placed inside of double quotes.

This will give you a GO TO combo box:

Using the combo box wizard will get you most of the way there. And then you just edit the macro with a small change.

Start by deleting your Go To and combo box.

Grab a combo box off the tool bar and add it to your header. When you do that, you should get a combo box wizard that offers three options: pull from a table, add your own entries, or find a record on your form. Select the third option. (You can put the combo box in the body of your form and still get the third option but I like it in the header.)

Go through the wizard and select your key field. Type in Go To: as the label of your combo box.

This may be all you need for your preference. Try it out. My problem is that the combo box stays filled and if you move to a different record, the combo box does not update automatically. So instead, I edit the query and set the value of the combo box to blank (I would use 0 if it was a number field).

To edit the macro, open the properties of your combo box and choose the Event tab. There is an embedded macro on the After Update Event. Click on the embedded macro and click the three dots to the right to edit the macro.

Once the macro opens, you have a SearchForRecord option at the top. Under it, there is an "Add New Action" drop-down.

In the "Add New Action", select SetProperty. That will open and allow you to enter values that you want to use.

For Control Name, select the name of your combo box. If you don't know, the name is displayed in the tab at the top. For Property, select Value. Then in Value, enter "". This will blank the combo box. Click Close at the top and you will be asked to Save.

This should give you a combo box that moves to the selected record and then blanks out the combo box.

Title: Finding Specific Text Records